【问题标题】:Convert a cURL command with the GET and data-urlencode options to javascript using node-fetch使用 node-fetch 将带有 GET 和 data-urlencode 选项的 cURL 命令转换为 javascript
【发布时间】:2018-12-30 10:17:55
【问题描述】:

我正在使用 node.js 和 express.js 开发一个网络应用程序。

这是我想使用 node-fetch 模块转换为 javascript 的有效 cURL 命令。

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

如何将data-urlencode参数放入下面的sn-p来完成javascript来完成这项工作?

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

【问题讨论】:

  • 在你的JS代码中的url之后,用“?”开始一个查询字符串然后将您的参数作为 KV 对添加到 queryString... $K=$V for limit 和 for count 。请注意,只有值需要 urlencoding ,而不是键。然后将您的 queryString 附加到主 URL,您应该很好地使用 fetch.GET

标签: javascript node.js curl node-fetch


【解决方案1】:

使用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'
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 2016-10-24
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    相关资源
    最近更新 更多