Reputation:
With Postman I can do a Get and send Auth-Token
as Header key , and then a value like Z4TTHmY98=gFw2rG
and It goes into web api method just fine as it is checked against a custom attributes.
However, I want to use the browser
Here is the api that is hit
http://localhost:29001/api/test
My Header Key = Auth-Token
My Header value = Z4TTHmY98=gFw2rG
After reading some articles, I thought this would work but header is null in stepping through debug of web api method.
Is it possible to do with browser of not? if so , how?
http://localhost:29001/api/test?Auth-Token=Z4TTHmY98=gFw2rG
Upvotes: 0
Views: 4545
Reputation: 41
AJAX request by page can use like ajaxSetup
+ beforeSend
:
$.ajaxSetup({
beforeSend: function(xhr) {
xhr.setRequestHeader('Auth-Token', 'Z4TTHmY98=gFw2rG');
}
});
Information
if you want to call third party API with same then you can use like
objHttpClient.DefaultRequestHeaders.TryAddWithoutValidation("Key", "value");
objHttpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "zMXGa8WNQuaBxWazgUhO2dw=");
Upvotes: 0
Reputation: 23788
Browser opening page doesn't send custom headers. But you can send AJAX request with custom headeres you need. Following example uses jQuery.ajax
$.ajax({
url: '/api/test',
headers: { 'Auth-Token': 'Z4TTHmY98=gFw2rG'}
});
Update
If you want to send the same headers with each AJAX request by your page you can use ajaxSetup
+ beforeSend
:
$.ajaxSetup({
beforeSend: function(xhr) {
xhr.setRequestHeader('Auth-Token', 'Z4TTHmY98=gFw2rG');
}
});
Upvotes: 1
Reputation: 5085
Use Webapi 2 Parameter binding.
You can make a call from browser as below
http://localhost:29001/api/test/Z4TTHmY98=gFw2rG/
[HttpGet]
[Route("/test/{AuthToken}")
public IHttpActionResult Test([FromUri] string AuthToken)
{
//Do whatever you want to do.
}
Careful with the route. It needs to be proper and configured in webapiconfig
.
Upvotes: 0