Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Http post works in Postman but not in C# with HttpClient

If I made a Http (Https) post through Postman everything works fine. But if I tried the same post with the HttpClient in .NET core it won't work it seems like the Authorization failed or something like that because the response.StatusCode is OK but if I read the content string I get a HTML page with 404 and its asking me if I like to login.

Of course I already checked the obvious things like the address and I tried different tokens (they always worked in postman). I also tried different versions of adding the Bearer token:

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

This is the code at the moment:

client.BaseAddress = new Uri("www.baseaddress.de");
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
var content = new StringContent(JsonConvert.SerializeObject(new { id = object.Id }), Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync($"/address", content);

Postman Headers: Postman headers

The body:

Postman body

I also tried it with insomnia and it works perfectly there too. Maybe postman do something in its header which I don't know? What are the common differences between postman an HttpClient? Does anyone have any other suggestion about what I could do or try to find the mistake?

Edit:

Fixed it by adding the / behind the base address and deleting it in front of the call:

client.BaseAddress = new Uri("www.baseaddress.de/");
HttpResponseMessage response = await client.PostAsync($"address", content);

Maybe anyone knows why the / is needed at the end of the base address, why can't I put it before the API call?

like image 913
Genfood Avatar asked Apr 04 '18 10:04

Genfood


People also ask

What is HTTP Post in C#?

HttpGet and HttpPost are both the methods of posting client data or form data to the server. HTTP is a HyperText Transfer Protocol that is designed to send and receive the data between client and server using web pages.

What is post method in Postman?

Postman POST request allows appending data to the endpoint. This is a method used to add information within the request body in the server. It is commonly used for passing delicate information. Once we send some the request body via POST method, the API in turn yields certain information to us in Response.

How do I test a Postman with a post request?

If you have a request you want to run, you will need to know the URL, method, and other optional values such as auth and parameters. To test sending a request in Postman, you can set the URL to the Postman Echo sample API endpoint https://postman-echo.com/get and the method to GET , then select Send.


1 Answers

Instead of new { id = object.Id }.ToString() you need to do

JsonConvert.SerializeObject(new { id = object.Id })

Code like new { id = 77 }.ToString() produces "{ id = 77 }", not "{ id: "77" }"

like image 80
Set Avatar answered Sep 24 '22 08:09

Set