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.
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.
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.
'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.
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.
You can use the following tool to convert and get the code:
https://curl.trillworks.com/#node
They support:
You can also do it using postman if you have. (late answer but might help someone else) 1. import curl command into the postman
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With