简体   繁体   中英

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

curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}'....

It works if I use this in the console. How can I implement this in JS code?

Unless there's something I'm missing about curl, you could use fetch to make a similar request:

 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:

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

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

with both you will recreate easily curl request you want.

of course this will require nodeJS application and cannot be used in client-side javascript code.

You can implement this in many different ways, either by using one of the libraries such as Axios or Jquery . Or using Javascript built in ways such as fetch , or even building your own XMLHttpRequest()

I believe the easiest way for you would be to use 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.

Have fun.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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