简体   繁体   English

需要帮助来诱导curl在代码JS / Node.js中发布

[英]Need help to induction curl Post in code JS / Node.js

curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}'.... curl -X POST -H'内容类型:application / json'--data'{“ text”:“你好,世界!”}'....

It works if I use this in the console. 如果我在控制台中使用它,它将起作用。 How can I implement this in JS code? 如何在JS代码中实现呢?

Unless there's something I'm missing about curl, you could use fetch to make a similar request: 除非我对curl不满意,否则可以使用fetch发出类似的请求:

 fetch(urlToPostTo, { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({text: 'Hello, World!'}) }); 

In your node application use proper package for curl like: 在您的节点应用程序中,使用适当的包进行curl,例如:

https://www.npmjs.com/package/curl-request https://www.npmjs.com/package/curl-request

https://www.npmjs.com/package/node-curl https://www.npmjs.com/package/node-curl

with both you will recreate easily curl request you want. 两者都可以轻松地重新创建所需的curl请求。

of course this will require nodeJS application and cannot be used in client-side javascript code. 当然,这将需要nodeJS应用程序,并且不能在客户端javascript代码中使用。

You can implement this in many different ways, either by using one of the libraries such as Axios or Jquery . 您可以通过多种方式来实现此目的,方法是使用AxiosJquery之类的库之一。 Or using Javascript built in ways such as fetch , or even building your own XMLHttpRequest() 或者使用以诸如fetch之类的方式构建的Javascript,或者甚至构建自己的XMLHttpRequest()

I believe the easiest way for you would be to use Axios. 我相信最简单的方法是使用Axios。 Of course, you can experiment with others as well. 当然,您也可以尝试与他人合作。

I will not post examples how to make a GET request, since the links I provided have examples already. 我不会发布如何发出GET请求的示例,因为我提供的链接已经包含示例。

Have fun. 玩得开心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM