alextc
alextc

Reputation: 3515

Convert a cURL command with the GET and data-urlencode options to javascript using node-fetch

I am developing a web app using node.js and express.js.

Here is the working cURL command that I would like to convert to javascript using the node-fetch module.

curl -X GET -H "X-Parse-Application-Id: APPLICATION_ID" -H "X-Parse-REST-API-Key: restAPIKey" -G --data-urlencode "count=1" --data-urlencode "limit=0" http://localhost:1337/parse/classes/GCUR_LOCATION

How to get the data-urlencode params into the following snippet to complete the javascript to do the job?

fetch('http://localhost:1337/parse/classes/GCUR_LOCATION', { method: 'GET', headers: {
    'X-Parse-Application-Id': 'APPLICATION_ID',
    'X-Parse-REST-API-Key': 'restAPIKey'
}});

Upvotes: 1

Views: 1958

Answers (1)

Andy Gaskell
Andy Gaskell

Reputation: 31761

Use querystring.

const querystring = require('querystring');

const query = querystring.stringify({ count: 1, limit: 0 });
const url = `http://localhost:1337/parse/classes/GCUR_LOCATION?${query}`;
fetch(url, { 
    method: 'GET', 
    headers: {
        'X-Parse-Application-Id': 'APPLICATION_ID',
        'X-Parse-REST-API-Key': 'restAPIKey'
    }
});

Upvotes: 2

Related Questions