1

I am calling an API from node.js. The documentation specified a query option appended to the url.

The API documentation shows something like this:

curl -L -X POST https://API_URL_GOES_HERE/library?format=txt -H "Authorization: Bearer AUTHCODE_GOES_HERE" -F [email protected] -F config="<config.json"

Using node-libcurl I am able to pass the query parameter over in the url as I set the options for the call:

myurl = "API_URL_GOES_HERE" + "/library?format=txt"; curl.setOpt('URL', myurl);

etc,etc

works just fine...

But when I add the node-fetch library and try:

fetch(myurl, { method: 'get', body: null, headers { 'Authorization': 'Bearer $(authcode)' } } .then ......

The API gives me the appropriate response in json but ignores the ?format=txt query parameter. My understanding is (from an earlier post here that fetch passes over the url exactly as it is presented. Any suggestions why node-libcurl works and node-fetch method doesn't??

I should point out I'm testing using V2.6 of node-fetch as I'm working on a localhost and can't use ESM. Which may be a bit unfair on node-fetch but I couldn't see anything in the documentation suggesting V3 might be different in this area.

1 Answer 1

1

After reading the V2 change log more carefully I found the issue had been fixed in September and node-fetch v2.6.4 contains this fix.

I upgraded the node-fetch library and my code works perfectly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.