Reputation: 1125
I am doing a simple call to an api giving it a token:
var url = baseUrl + "buildings";
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = await client.GetAsync(url);
string content = (response.Content.ReadAsStringAsync().Result);
From the debugger I know that the header looks like this:
I know for a fact that the token is correct.
Here is the same in Postman which works:
Why is postman working and c# isnt? The header should be the same. The returned message from the server is: JWT TOKEN NOT FOUND
EDIT.
its gets more wired: I am able to access another api endpoint (different url, same base url) with the exact code as posted and the same token. This works fine, but the base url returns TOKEN NOT FOUND, again, only not working in c#, while working from postman..?!?
Upvotes: 1
Views: 968
Reputation: 1125
My code is correct. It turns out that the URL had to end on a "/" (slash). Allthough postman did the auto generated code WITHOUT the "/", it is neccissarry in c#. Dont ask,...
Upvotes: 3
Reputation: 3183
Because it's not an authorization or authentication header, it's just a header with the key "Authorization".
Change this line:
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
to this:
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");
You can actually see that in Postman as well, in Postman there is a tab authorization which is (most likely) empty since there are no auth-headers present in the request.
Upvotes: 0