node-fetch icon indicating copy to clipboard operation
node-fetch copied to clipboard

query string parameters on GET

Open AjRoAs opened this issue 10 years ago • 16 comments

hi, i just read that it's no supported by node-fetch. i'm trying to consume an API that only works with GET so y cannot use the POST body. Any ideas of how to solve this?

AjRoAs avatar Oct 14 '16 11:10 AjRoAs

You might want to explain it with a bit of code, there are really 3 ways to append data: as url, as headers and as body.

bitinn avatar Oct 14 '16 11:10 bitinn

On similar lines how do I attach query paramets to a get url? is this correct?

  return fetch('https://graph.facebook.com/v2.6/me/messages', {
        method: 'GET',
        headers: {'Content-Type': 'application/json'},
        qs: {access_token: PAGE_ACCESS_TOKEN},
    })

crosofg avatar Oct 14 '16 11:10 crosofg

I get this error

{ message: 'An active access token must be used to query information about the current user.',
2016-10-14T11:47:44.862805+00:00 app[web.1]:      type: 'OAuthException',
2016-10-14T11:47:44.862805+00:00 app[web.1]:      code: 2500,
2016-10-14T11:47:44.862806+00:00 app[web.1]:      fbtrace_id: 'D3AXnal4LBE' } }

crosofg avatar Oct 14 '16 11:10 crosofg

im having this exact problem fetch('shittyApi/', { method: 'GET', headers: {'Content-Type': 'application/json'}, qs: {param1: "string"}, }) and the qs just is not pased. y also tried pasing to string and atach it in the end of the url

AjRoAs avatar Oct 14 '16 12:10 AjRoAs

Huh, where do you guys find the tutorial that it says Fetch accepts qs as an option? It doesn't, Fetch is a low-level request module like request.

If you have to do this by hand, I assume you need to send a header named "access_token" (maybe named differently, depending on oauth version and provider), but find that oauth header name, and set your token as a value.

But you probably shouldn't do oauth with fetch anyway. There are many oauth modules that help you with it. For example, grant.

bitinn avatar Oct 14 '16 12:10 bitinn

I just have to pass access_token: PAGE_ACCESS_TOKEN as query parameter. How do i do it?

crosofg avatar Oct 14 '16 12:10 crosofg

You append it to the url

bitinn avatar Oct 14 '16 12:10 bitinn

when appended to url catch this err in the fetch req :

ERROR TypeError: Cannot read property '1' of undefined
    at /Users/democles/remux/build/webpack:/data/queries/studies.js:59:28
    at Array.map (native)
    at /Users/democles/remux/build/webpack:/data/queries/studies.js:53:24
    at runCallback (timers.js:574:20)
    at tryOnImmediate (timers.js:554:5)
    at processImmediate [as _immediateCallback] (timers.js:533:5)
From previous event:
    at Promise.then (/Users/democles/remux/node_modules/sequelize/lib/promise.js:21:17)
    at resolve (/Users/democles/remux/build/webpack:/data/queries/studies.js:52:10)
    at resolveOrError (/Users/democles/remux/node_modules/graphql/execution/execute.js:454:12)
    at resolveField (/Users/democles/remux/node_modules/graphql/execution/execute.js:445:16)
    at /Users/democles/remux/node_modules/graphql/execution/execute.js:252:18
    at Array.reduce (native)
    at executeFields (/Users/democles/remux/node_modules/graphql/execution/execute.js:249:42)
    at executeOperation (/Users/democles/remux/node_modules/graphql/execution/execute.js:190:10)
    at /Users/democles/remux/node_modules/graphql/execution/execute.js:116:13
    at execute (/Users/democles/remux/node_modules/graphql/execution/execute.js:115:10)
    at /Users/democles/remux/node_modules/express-graphql/dist/index.js:169:37
    at process._tickCallback (internal/process/next_tick.js:103:7)

AjRoAs avatar Oct 14 '16 12:10 AjRoAs

Your call stack doesn't contain node-fetch, this is not our error.

bitinn avatar Oct 14 '16 12:10 bitinn

url string will look messy :|. but okay works for me.

crosofg avatar Oct 14 '16 12:10 crosofg

solved, i ended passing the params in the url. the previous error was an stupid promise in other function that somehoy affected the fetch promise.

AjRoAs avatar Oct 14 '16 14:10 AjRoAs

Was also looking for this as it's possible in the official fetch impl:

https://fetch.spec.whatwg.org/#fetch-blob-example

var url = new URL("https://geo.example.org/api"),
    params = {lat:35.696233, long:139.570431}
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]))
fetch(url).then(/* … */)

Which would save me from doing a cheap parser of my own :)

intellix avatar Nov 13 '16 16:11 intellix

@intellix, the URL class referenced in the spec is defined in the WHATWG URL Standard, which in Node.js is implemented as whatwg-url module, and as an experimental feature in Node.js 7.0.0. However, currently the latter does not yet implement the full specification (pending PR in nodejs/node#9484), so you'll have to use whatwg-url in order to achieve that currently.

TimothyGu avatar Nov 13 '16 17:11 TimothyGu

Have a look at the built in node module querystring to easily create a query string from an object.

charlesbdudley avatar Apr 24 '17 18:04 charlesbdudley

I was also looking for a way to specify GET query string parameters as an object and ended up creating a convenience wrapper to do it: fetch-json

The example above would look something like:

const url = 'https://graph.facebook.com/v2.6/me/messages';
const params = { access_token: PAGE_ACCESS_TOKEN };
fetchJson.get(url, params).then(console.log);

pilafmon avatar Aug 10 '18 21:08 pilafmon

@dpilafian

const url = 'https://graph.facebook.com/v2.6/me/messages?'
const params = new URLSearchParams({ access_token: 'PAGE_ACCESS_TOKEN' })
fetch(url + params).then(console.log)

jimmywarting avatar Aug 10 '18 21:08 jimmywarting