0

Good morning,

I have a big problem with routing (I guess) for http Post calls. I use .Net 6.

I have the following call which works fine:

HttpResponseMessage response1 = await client.GetAsync("http://localhost:6000/api/" + "Utilisateur/AjouterUtilisateur1/" + "Machaine");

On the UserController class, I have the following code:

[Route("api/[controller]")]
[ApiController]
public class UtilisateurController : Controller
 
[HttpPost("AjouterUtilisateur1/{jsonArgs}")]
public ActionResult<string> AjouterUtilisateur1(string jsonArgs)       
{ … }

That works well. Now I would like to do a Post instead of the Get. So on the caller, I have the code:

HttpContent httpContent1 = new StringContent("MaChaine", Encoding.UTF8);
HttpResponseMessage response1 = await client.PostAsync("http://localhost:6000/api/" + "Utilisateur/AjouterUtilisateur1", httpContent1);

And on the server, I have the code:

[Route("api/[controller]")]
ApiController]
public class UtilisateurController : Controller
 
[HttpPost("AjouterUtilisateur1")]
public ActionResult<string> AjouterUtilisateur1(string jsonArgs)       
{ … }

The class constructor is well executed but that's it. This gives me the following error in response1 (at the caller level):

{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers: { Date: Fri, 14 Jul 2023 17:37:53 GMT Server: Kestrel Transfer-Encoding: chunked Content-Type: application/problem+json; charset=utf-8 }}

Thanks for your help !

Can you help me find my error please?

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • have you tried this `HttpContent httpContent1 = new StringContent("\"MaChaine\"", Encoding.UTF8, "application/json");`? and also change the parameter to `([FromBody] string jsonArgs)` – Vivek Nuna Jul 15 '23 at 10:12
  • Yes it works ! Thank you so much ! My original problem was sending a dto structure. I modified the program like this but I still have the same error: string jsonEnvoi1 = JsonSerializer.Serialize(dtoUtilisateur); HttpContent httpContent1 = new StringContent(jsonEnvoi1, Encoding.UTF8, "application/json"); – Philippe Lambert Jul 15 '23 at 10:38
  • I think, No need to again convert to HttpContent then. You already have the json string. So send it directly – Vivek Nuna Jul 15 '23 at 11:06
  • Yes, everything is ok! Thank you very much Vivek Nuna! So I removed the httpContent1 and changed PostAsync to PostAsJsonAsync and everything works! Thanks Vivek! – Philippe Lambert Jul 15 '23 at 11:25

1 Answers1

0

A bad request means that you are not sending the payload as its required by the backend, so you need to make the below changes.

you can send the JSON string like the one below.

HttpContent httpContent1 = new StringContent("\"MaChaine\"", Encoding.UTF8, "application/json");
HttpResponseMessage response1 = await client.PostAsync("http://localhost:6000/api/" + "Utilisateur/AjouterUtilisateur1", httpContent1);

and If you have a dto object and you can serialize it to a JSON string and use the PostAsJsonAsync to send the JSON.

string jsonEnvoi1 = JsonSerializer.Serialize<dtoUtilisateur>(dtoUtilisateur); HttpContent httpContent1 = new StringContent(jsonEnvoi1, Encoding.UTF8, "application/json");
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197