Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: only absolute urls are supported #481

Closed
lemoustachiste opened this issue Jul 5, 2018 · 15 comments
Closed

Error: only absolute urls are supported #481

lemoustachiste opened this issue Jul 5, 2018 · 15 comments

Comments

@lemoustachiste
Copy link

@lemoustachiste lemoustachiste commented Jul 5, 2018

This happens when you are trying to get a relative path is ../../my/path/to/myfile.json. This is supported in browsers (tested on Firefox, Chrome and Safari).

My issue is in testing, tests fail because of the above error, which is annoying because now I have a distinction between the browser and the node environment running the tests.

This is the stack trace beyond my code:

      at node_modules/node-fetch/index.js:54:10
      at new Fetch (node_modules/node-fetch/index.js:49:9)
      at Fetch (node_modules/node-fetch/index.js:37:10)
      at Object.<anonymous>.module.exports (node_modules/isomorphic-fetch/fetch-npm-node.js:8:19)

I have added a comment in isomorphic-fetch discussion: matthew-andrews/isomorphic-fetch#76 (comment), but would like to attract attention on this use case here too.

@nenadjaja
Copy link

@nenadjaja nenadjaja commented Jul 9, 2018

I am getting the same error when trying to run tests (using Enzyme and Ava):

Rejected promise returned by test. Reason:

  Error {
    message: 'only absolute urls are supported',
  }

  node_modules/node-fetch/index.js:54:10
  new Fetch (node_modules/node-fetch/index.js:49:9)
  Fetch (node_modules/node-fetch/index.js:37:10)
  node_modules/fetch-ponyfill/fetch-node.js:18:12
  _callee$ (frontend/src/adapters/http.js:28:20)
  makeRequest (frontend/src/adapters/http.js:43:17)
  _callee2$ (frontend/src/adapters/http.js:54:20)

@jimmywarting
Copy link
Collaborator

@jimmywarting jimmywarting commented Jul 9, 2018

The server don't have an origin so the default domain name is unknown. Therefore relative url won't work. Hence the error.

Supporting file:// protocol is also out of the question.

I would like to close this issue as relative to
#75

@TimothyGu
Copy link
Collaborator

@TimothyGu TimothyGu commented Jul 9, 2018

Agreed with @jimmywarting. You may want to use new URL(relativeURL, baseURL) to resolve the URL before passing it to node-fetch.

@mariothedev
Copy link

@mariothedev mariothedev commented Oct 21, 2019

maybe use fetch('http://localhost:3000',...) instead of fetch('/someEndpoint', ...)

@avdaredevil
Copy link

@avdaredevil avdaredevil commented Feb 28, 2020

@kopax I was having the same issue, it seems to be some issues with the types files. I fixed my issue which was that I was using it like:

fetch({url, headers: {Authorization: `token ${process.GIT_TOKEN}`}})

instead of

fetch(url, {headers: {Authorization: `token ${process.GIT_TOKEN}`}})

@ReubenMathew
Copy link

@ReubenMathew ReubenMathew commented Jun 22, 2020

How would you fix this in nextjs when using getStaticPaths() and getStaticProps() ?

@PriimeTime
Copy link

@PriimeTime PriimeTime commented Jun 23, 2020

@Monster880
Copy link

@Monster880 Monster880 commented Oct 19, 2020

It looks like there's something wrong with the URL in the fetch method.
For me, it solved by changing url in the fetch method.
Pay attention to the correctness of the URL address.

@dork1
Copy link

@dork1 dork1 commented Oct 27, 2020

@Monster880 How did you change the url it to make it work? I am having the same problem.

@jimmywarting
Copy link
Collaborator

@jimmywarting jimmywarting commented Oct 28, 2020

@dork1

const baseURL = 'http://localhost'
fetch(new URL(url, baseURL))

@dork1
Copy link

@dork1 dork1 commented Oct 28, 2020

Thanks @jimmywarting - could you advise what I might be missing?

    function updateDisplayName() {
        const baseURL = 'http://localhost'; // 'http://localhost:8000';
        const url = process.env.AUTHO_POST_ENDPOINT;

    fetch(new URL(url, baseURL, {
        method: 'POST',
        headers: {authorization: 'Bearer ' + process.env.AUTHO_TOKEN},
        data: '{"user_metadata": {"displayName": "FooName"}',
    }))
        .then(function(response) {
            console.log('update', response.data);
        }).catch(function(error) {
            console.error(error);
        });
    };

@jimmywarting
Copy link
Collaborator

@jimmywarting jimmywarting commented Oct 28, 2020

response.data is wrong use response.body instead if you intend to use the response stream and pipe it
if you need to get the response data then you need to call either

  • await response.json(),
  • await response.text() or
  • await response.arrayBuffer()

there is nothing in the spec that defines .data

same with request body: it should be

    fetch(new URL(url, baseURL, {
        method: 'POST',
        headers: {authorization: 'Bearer ' + process.env.AUTHO_TOKEN},
-        data: '{"user_metadata": {"displayName": "FooName"}',
+        body: '{"user_metadata": {"displayName": "FooName"}',
    }))

@dork1
Copy link

@dork1 dork1 commented Oct 28, 2020

@jimmywarting Thank you! I am now getting a response!

@hinstw
Copy link

@hinstw hinstw commented Feb 8, 2021

Would be nice if the error message saying only absolute urls are supported included the actual URL and consequentially spared a bit of headache for the users

HerCerM added a commit to HerCerM/GDHE that referenced this issue Jun 16, 2021
This is necessary because node-fetch does not support relative urls:
node-fetch/node-fetch#481

More on node-fetch:

- Use node-fetch for Node.js tests, since Node.js does not implement
  fetch API. Injecting the implementation was not possible since fetch
  seems to require the owner object to implement the Window interface.
  On further study it might be possible.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet