0

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?

Amiga500
  • 5,874
  • 10
  • 64
  • 117
Arek Szumacher
  • 348
  • 1
  • 4
  • 14
  • 2
    Possible duplicate of [Curl equivalent in nodejs?](https://stackoverflow.com/questions/6819143/curl-equivalent-in-nodejs) – Lex Lustor Nov 30 '18 at 08:53

3 Answers3

1

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!'})
  });
OliverRadini
  • 6,238
  • 1
  • 21
  • 46
0

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.

Łukasz Blaszyński
  • 1,536
  • 1
  • 8
  • 13
0

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.

Community
  • 1
  • 1
Amiga500
  • 5,874
  • 10
  • 64
  • 117