I'm encountering a problem where axios doesn't seem to send custom headers with my requests.
I'm using it like this:
axios({
method: 'get',
url: 'www.my-url',
headers: { 'Custom-Header': 'my-custom-value' }
})
However, looking at the actual request that is sent to the server, the custom header doesn't seem to be anywhere.
REQUEST HEADERS:
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: es-ES,es;q=0.9
Access-Control-Request-Headers: custom-header
Access-Control-Request-Method: GET
Connection: keep-alive
Host: my-url
I suspect it might be a CORS problem (the response headers don't include Custom-Header in Access-Control-Allow-Headers), but I would like to make sure before contacting the API owners about the matter.
I'm encountering a problem where axios doesn't seem to send custom headers with my requests.
I'm using it like this:
axios({
method: 'get',
url: 'www.my-url.',
headers: { 'Custom-Header': 'my-custom-value' }
})
However, looking at the actual request that is sent to the server, the custom header doesn't seem to be anywhere.
REQUEST HEADERS:
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: es-ES,es;q=0.9
Access-Control-Request-Headers: custom-header
Access-Control-Request-Method: GET
Connection: keep-alive
Host: my-url.
I suspect it might be a CORS problem (the response headers don't include Custom-Header in Access-Control-Allow-Headers), but I would like to make sure before contacting the API owners about the matter.
Share Improve this question asked May 30, 2018 at 11:38 NinethousandNinethousand 5352 gold badges7 silver badges16 bronze badges 8- Do you get a CORS related error message printed on the console? – Quentin Commented May 30, 2018 at 11:40
- Does the browser make a GET request or is it a preflight OPTIONS request? – Quentin Commented May 30, 2018 at 11:40
- The Access-Control-Request-Headers request header is used when issuing a preflight request to let the server know which HTTP headers will be used when the actual request is made. Why are you sending your custom header as access control ? – Ronn Wilder Commented May 30, 2018 at 12:11
- can you please show what exactly is your call, all the request headers you are sending. – Ronn Wilder Commented May 30, 2018 at 12:11
-
@RonnWilder — Eh? The OP is trying to send a
Custom-Header
header. Why wouldn't the browser make a preflight request with aAccess-Control-Request-Headers: custom-header
header ahead of making the request the OP is trying to make? – Quentin Commented May 30, 2018 at 12:12
2 Answers
Reset to default 13 +50Ok. so your case is preflight request. when client tries to send a custom header, server needs to verify that it accepts that header.
so in that case, a preflight options request is sent with header Access-Control-Request-Headers. if server responds that it will accept the custom header. then actual request will be sent.
in your case server response header - access-control-allow-headers does not contains your custom header name. thats why it failed.
Note: the actual POST request does not include the Access-Control-Request-* headers; they are needed only for the OPTIONS request.Read this article for more explanation - cors - options call
Your axios request is correct. You need to allow your custom headers on server side. If you have your api in php then this code will work for you.
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, HEAD");
header("Access-Control-Allow-Headers: Content-Type, Custom-Header");
Once you will allow your custom headers on server side, your axios requests will start working fine.