Redirect (301) aren't followed
Hi,
the server is configured to redirect all traffic from http to https.
Example: [http://v22017084844652188.powersrv.de/schemas/test.json](HTTP address).
Calling this
parser.dereference("http://v22017084844652188.powersrv.de/schemas/test.json")
.then((value) => {
console.log("Schema fetched...");
this.parserSchema = value;
})
.catch(error => {
console.log("Schema not fetched...");
console.log(error);
this.parserError = error;
});
I'm getting the following error (running into a CORS problem):
Failed to load http://v22017084844652188.powersrv.de/schemas/test.json: Redirect from 'http://v22017084844652188.powersrv.de/schemas/test.json' to 'https://v22017084844652188.powersrv.de/schemas/test.json' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
A redirect has to be detected and the the ressource should be dereferenced! @BigstickCarpet Should the redirects handled in http.js?
@big-r81 - Thanks for opening this issue.
Yes, the redirects should be handled in http.js. Is this only happening in Node, or also in web browsers? I'm pretty sure that web browsers automatically follow redirects, so we only need to fix this for Node. If that's the case, then we can just use follow-redirects as a drop-in replacement for the default http and https modules.
@BigstickCarpet - Hi,
if i open the url in the browser, i get redirected to https. I use your library in an ionic/angular project and there i running into the problem...
@BigstickCarpet
I'm not a node guru, so i tried the following:
- clone this repo
- changed http.js
var http = require('follow-redirects').http,
https = require('follow-redirects').https,
...
- run
npm run build(FYI: it will break the redirect tests...) -
npm link(in the json-schema-ref-parser directory) - in my own project dir
npm link json-schema-ref-parser - starting my app, but the same result...
@big-r81 - ok, thanks for the analysis. Looks like this isn't going to be as straightforward as I had hoped. :-/
May not be a perfect solution, but I got around the issue of redirects by handling the 301 inside a custom fetch resolver and returning a new $ref from it (with the redirect target). The code then naturally follows that new $ref.