query string parameters on GET
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?
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.
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},
})
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' } }
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
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.
I just have to pass access_token: PAGE_ACCESS_TOKEN as query parameter. How do i do it?
You append it to the url
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)
Your call stack doesn't contain node-fetch, this is not our error.
url string will look messy :|. but okay works for me.
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.
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, 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.
Have a look at the built in node module querystring to easily create a query string from an object.
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);
@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)