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:
The 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?
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.
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.
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.
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" }"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With