Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert cURL request to request of node.js

Here is my valid cURL command:

curl 'https://www.example.com/api/' --data '{"jsonrpc":"2.0","method":"getObjectsByFilter","id":"3"}'

here is what I tried in Node.js:

var url = 'https://www.example.com/api/';

var data = {
  "jsonrpc":"2.0",
  "id":"3"
};

req.post({ url: url, form: data}, function (err, result, body) {

But it is not valid.

like image 328
Ilshat Avatar asked May 27 '15 19:05

Ilshat


People also ask

How do I change my curl to request?

Convert Curl command to the HTTP request using ReqBin. Enter your Curl request, click the Submit button to check if you entered the Curl command correctly, and then switch to the Raw tab to see the generated HTTP request.

How do I send a curl request in node JS?

We are going to use the Curl() class provided by the library to perform the form requests. To start, create a JavaScript file with your preferred name then write the following snippet in it. const querystring = require("querystring"); const { Curl } = require("node-libcurl"); const terminate = curlTest. close.

Is curl a request?

'cURL' is a command-line tool that lets you transmit HTTP requests and receive responses from the command line or a shell script. It is available for Linux distributions, Mac OS X, and Windows. To use cURL to run your REST web API call, use the cURL command syntax to construct the command.

How do I convert curls to Python requests?

Curl Converter automatically generates valid Python code using the Python request library for all provided Curl HTTP headers and Curl data. Enter the Curl command, click Run to execute the command online and check the results. Click Generate Code and select Python to convert the Curl command to Python code.


3 Answers

You can use the following tool to convert and get the code:

https://curl.trillworks.com/#node

They support:

  • Node.js
  • Python
  • PHP
like image 153
Harshal Y. Avatar answered Oct 13 '22 22:10

Harshal Y.


You can also do it using postman if you have. (late answer but might help someone else) 1. import curl command into the postman enter image description here

  1. After import click on the code and you can see many options to generate the code for different languages. enter image description here
like image 21
AD14 Avatar answered Oct 13 '22 23:10

AD14


You are going to need to install the npm module request

If you have npm installed already, just run the following command:

npm install request

Make sure you require the module at the top of your node file, like so

var request = require('request');

You can use the module with the following:

var request = require('request');

var url = 'https://www.example.com/api/';

var data = {
  "jsonrpc":"2.0",
  "id":"3"
};

request.post({url:url, formData: data}, function(err, httpResponse, body) {
  if (err) {
    return console.error('post failed:', err);
  }

  console.log('Post successful!  Server responded with:', body);
});

Check the documentation out for more information:

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

like image 5
Arian Faurtosh Avatar answered Oct 14 '22 00:10

Arian Faurtosh