json-schema-ref-parser icon indicating copy to clipboard operation
json-schema-ref-parser copied to clipboard

Redirect (301) aren't followed

Open big-r81 opened this issue 8 years ago • 5 comments

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 avatar Nov 06 '17 13:11 big-r81

@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.

JamesMessinger avatar Nov 06 '17 14:11 JamesMessinger

@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...

big-r81 avatar Nov 06 '17 14:11 big-r81

@BigstickCarpet

I'm not a node guru, so i tried the following:

  1. clone this repo
  2. changed http.js
var http = require('follow-redirects').http,
    https = require('follow-redirects').https,
    ...
  1. run npm run build (FYI: it will break the redirect tests...)
  2. npm link (in the json-schema-ref-parser directory)
  3. in my own project dir npm link json-schema-ref-parser
  4. starting my app, but the same result...

big-r81 avatar Nov 06 '17 16:11 big-r81

@big-r81 - ok, thanks for the analysis. Looks like this isn't going to be as straightforward as I had hoped. :-/

JamesMessinger avatar Nov 07 '17 10:11 JamesMessinger

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.

JaiPe avatar Feb 12 '22 11:02 JaiPe