Error: Cross origin http://localhost forbidden
See original GitHub issueHi All,
I’ve got lambda function which I access via API Gateway.
This function works ok when I test it via Postman or via this test which uses request
library:
it('shoudl work via request', async () => {
var options = { method: 'POST',
url: 'https://-----------.execute-api.eu-central-1.amazonaws.com/dev/generate',
headers:
{
'Cache-Control': 'no-cache',
'Content-Type': 'application/json' },
body:
{ html: 'PGgxPkhlbGxvIHt7bmFtZX19PC9oMT48aDI+SGVsbG8gd29ybGQhISE8L2gyPg==',
variables: 'eyJuYW1lIjoiV29ybGQiLCJwYXRoIjoiL2hvbWUvcGF3ZWwvLm52bS92ZXJzaW9ucy9ub2RlL3Y4LjExLjMvYmluOi91c3IvbG9jYWwvc2JpbjovdXNyL2xvY2FsL2JpbjovdXNyL3NiaW46L3Vzci9iaW46L3NiaW46L2JpbjovdXNyL2dhbWVzOi91c3IvbG9jYWwvZ2FtZXM6L3NuYXAvYmluOi91c3IvbGliL2p2bS9qYXZhLTgtb3JhY2xlL2JpbjovdXNyL2xpYi9qdm0vamF2YS04LW9yYWNsZS9kYi9iaW46L3Vzci9saWIvanZtL2phdmEtOC1vcmFjbGUvanJlL2JpbjovdmFyL3Rhc2sifQ==',
css: 'aDEge2NvbG9yOiByZWR9' },
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
expect(body.generated).toBeTruthy()
expect(body.pdf).toBeTruthy()
});
})
Problem
Unfortunately, when I’m using Axis I get following message Error: Cross origin http://localhost forbidden.
Could you please tell me what am I doing wrong?
My code looks like that:
it('should work via axios', async () => {
const event = {
html: "PGgxPkhlbGxvIHt7bmFtZX19PC9oMT48aDI+SGVsbG8gd29ybGQhISE8L2gyPg==",
variables: "eyJuYW1lIjoiV29ybGQiLCJwYXRoIjoiL2hvbWUvcGF3ZWwvLm52bS92ZXJzaW9ucy9ub2RlL3Y4LjExLjMvYmluOi91c3IvbG9jYWwvc2JpbjovdXNyL2xvY2FsL2JpbjovdXNyL3NiaW46L3Vzci9iaW46L3NiaW46L2JpbjovdXNyL2dhbWVzOi91c3IvbG9jYWwvZ2FtZXM6L3NuYXAvYmluOi91c3IvbGliL2p2bS9qYXZhLTgtb3JhY2xlL2JpbjovdXNyL2xpYi9qdm0vamF2YS04LW9yYWNsZS9kYi9iaW46L3Vzci9saWIvanZtL2phdmEtOC1vcmFjbGUvanJlL2JpbjovdmFyL3Rhc2sifQ==",
css: "aDEge2NvbG9yOiByZWR9"
}
await axios.post(url, event, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
responseType: 'json',
withCredentials: true,
})
.then(function (response) {
console.log(response);
// console.log('ok')
expect(response.generated).toBeTruthy()
expect(response.pdf).toBeTruthy()
})
.catch(function (error) {
console.log(error);
});
})
Context
- axios version: v0.18.0
- Environment: node v8.11.3
Issue Analytics
- State:
- Created 5 years ago
- Comments:11
Top Results From Across the Web
Cross origin http://localhost forbidden - in ReactJS/Jest test ...
The error happens because the external API you call during the tests has a CORS restriction different than Jest's default jsdom one has ......
Read more >Jest + Node + SharePoint + MSAL = Error: Cross origin http ...
If you are getting the following errors when you try and use MSAL with JEST to connect to SharePoint: Error: Cross origin http://localhost...
Read more >Cross origin http://localhost forbidden - in ReactJS/Jest test ...
The error happens because the external API you call during the tests has a CORS restriction different than Jest's default jsdom one has...
Read more >error cross origin http //localhost forbidden jest ...
Answers related to “error cross origin http //localhost forbidden jest”. How to by pass CORS error locally · access to xmlhttprequest at from...
Read more >CORS errors - HTTP - MDN Web Docs - Mozilla
Cross -Origin Resource Sharing (CORS) is a standard that allows a server to relax the same-origin policy. This is used to explicitly allow ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I also had this problem. This thread led me to this part of the Jest docs. I was able to fix the issue by pasting:
At the top of my test file. FWIW, adding:
to either
jest.config.js
or to mypackage.json
file did not work for some reason. Not sure why…really thanks, this solves my problem