4
I’m trying to request an API using Axios:
axios.post('http://api.teste.com.br/v1/getToken', {
withCredentials: true,
auth: {
username: 'usuario',
password: 'password'
},
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Authorization",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS, PUT, PATCH, DELETE" ,
"Content-Type": "application/json;charset=UTF-8"
},
}).then(resp => {
console.log(resp)
})
.catch(error => {
console.log(error)
})
But always presents the error:
Access to Xmlhttprequest at 'http://api.teste.com.br/v1/getToken' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight.
Is there any more configuration to do? Remembering that it is a third-party API and I can’t change the CORS on the server, and Postman works smoothly.
You’re calling
axios.post()
, would not beaxios.get()
?– Pedro Gaspar
I also found this strange as a post in the API documentation.
– Danilo Molina
The API probably does not accept the value entered in the "Content-Type".
– Valdeir Psr
Your api is on that?
– Sorack
the api is third party, do not know what language it was developed. More on Postman I pass the "Content-Type" as application/json and it works.
– Danilo Molina
The problem is that the CORS blocks the way you’re doing
– Sorack
That’s the problem is the CORS. I found the solution here : https://medium.com/@gigioSouza/resolvendo-o-problema-do-Cors-com-angular-2-e-o-angular-cli-7f7cb7aab3c2
– Danilo Molina
Install this plugin on Crome: Allow-Control-Allow-Origin: *, from time to time I need to enable it to work in my applications locally when I do Viacep queries
– Marconi
Tries to change the
Content-type
for"application/x-www-form-urlencoded; charset=UTF-8"
and see if anything changes. References: Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers and Differences in application/json and application/x-www-form-urlencoded.– Pedro Gaspar
Right, but the problem is that when you run npm run build, the proxy is ignored, and you get the same problem again. Until then you can’t find a solution to this, you’ve succeeded?
– Pedro Arvellos
I found it strange because the error response is that it does not support the Content-Type you passed.
– Carlos Querioz