Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用输入参数将curl转换为JS fetch()_Javascript_Fetch Api - Fatal编程技术网

Javascript 使用输入参数将curl转换为JS fetch()

Javascript 使用输入参数将curl转换为JS fetch(),javascript,fetch-api,Javascript,Fetch Api,我有一个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

我有一个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'
});

我在的文档中工作,但在计算如何传递关键字和限制参数时遇到了困难。我很感谢你的指导。(我也愿意使用fetch()以外的请求类型。)谢谢。

您可以使用
body
属性传递数据:

fetch("https://url.omitted", {
      method: 'post',
      headers: {
        'Content-Type': 'application/json',
      },
      body: {"keywords":["keyword1", "keyword2"], "limit": 10}
    })

如果您想使用另一个
HTTP客户端
,这是一个很好的客户端。

谢谢。我试图设置body属性的格式,这太复杂了。