使用输入参数将 curl 转换为 JS fetch()
Convert curl to JS fetch() with input paramters
我有一个 http API 我目前正在从命令行通过 curl 查询,我想通过我的 JS 应用程序中的 fetch() 调用它以进行测试。
我的 curl 命令如下所示:
curl -vvv -X POST -H "Content-Type: text/json" --data '{"keywords":["keyword1", "keyword2"], "limit": 10}' https://url.omitted
我想像这样获取它:
var queryURL = "https://url.omitted";
fetch(queryURL, {
method: 'POST'
});
我正在研究 https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch 的文档,但无法弄清楚如何传递关键字和限制参数。我会很感激一些指导。 (而且我也愿意使用 fetch() 以外的请求类型。)谢谢。
您可以使用 body
属性传递您的数据:
fetch("https://url.omitted", {
method: 'post',
headers: {
'Content-Type': 'application/json',
},
body: {"keywords":["keyword1", "keyword2"], "limit": 10}
})
如果您想使用另一个 HTTP Client
,axios 是个不错的选择。
我有一个 http API 我目前正在从命令行通过 curl 查询,我想通过我的 JS 应用程序中的 fetch() 调用它以进行测试。
我的 curl 命令如下所示:
curl -vvv -X POST -H "Content-Type: text/json" --data '{"keywords":["keyword1", "keyword2"], "limit": 10}' https://url.omitted
我想像这样获取它:
var queryURL = "https://url.omitted";
fetch(queryURL, {
method: 'POST'
});
我正在研究 https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch 的文档,但无法弄清楚如何传递关键字和限制参数。我会很感激一些指导。 (而且我也愿意使用 fetch() 以外的请求类型。)谢谢。
您可以使用 body
属性传递您的数据:
fetch("https://url.omitted", {
method: 'post',
headers: {
'Content-Type': 'application/json',
},
body: {"keywords":["keyword1", "keyword2"], "limit": 10}
})
如果您想使用另一个 HTTP Client
,axios 是个不错的选择。