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 data_file=@datafile.xxx -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.

Marlowe
  • 33
  • 4

1 Answers1

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.

Marlowe
  • 33
  • 4