I have to create an api in node js i am creating it into curl how can i convert the cURL Request into node js. how can i pass url,header and data binary in node js? The follwing is the cURL Request?
curl -v "https://us-extract.api.smartystreets.com/?auth-id=AUTH_ID&auth-token=AUTH_TOKEN" -H "content-type: application/json" --data-binary ""
There are some very handy tools for this, check out https://curl.trillworks.com/#node, it will convert curl requests to code in Node.js, Python, Go etc.
I've changed your curl request slightly to:
curl -v -X POST "https://us-extract.api.smartystreets.com/?auth-id=AUTH_ID&auth-token=AUTH_TOKEN" -H "content-type: application/json" --data-binary "1600 Amphitheatre Parkway,Mountain View, CA 94043"
(Note I've redacted your auth-id and auth-token, we don't other people to use these.. :-))
In your example the output will be like as below, note this uses the request library. You'll have to do an npm install request to add this to your project.
var request = require('request');
// Put your auth id and token here.
const AUTH_ID = "";
const AUTH_TOKEN = "";
var headers = {
'content-type': 'application/json'
};
var dataString = '1600 Amphitheatre Parkway,Mountain View, CA 94043';
var options = {
url: 'https://us-extract.api.smartystreets.com/?auth-id=' + AUTH_ID + '&auth-token=' + AUTH_TOKEN,
method: 'POST',
headers: headers,
body: dataString,
json: true // Set this to parse the response to an object.
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
// Log the API output.
(body.addresses || []).forEach((element, index) => {
console.log(`api_output (address #${index+1}):`, element.api_output);
});
}
}
request(options, callback);