2

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 ""
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
Mariam
  • 33
  • 1
  • 2
  • 7

1 Answers1

2

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);
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
  • 1
    Thank yo so much. problem solved to using this code – Mariam Oct 11 '19 at 11:40
  • 1
    i have a small doubt how can i get api_output details in json body iam using console.log(body.api_output); but the result is undefined – Mariam Oct 11 '19 at 11:42
  • Oh yes, I'll update the answer. If we add an option of json: true to the options object, we'll get back the response as an object rather than as a string. – Terry Lennox Oct 11 '19 at 11:45
  • I've updated the answer, each address has an api_output array that we can access as above. I believe this is what you need! – Terry Lennox Oct 11 '19 at 11:50
  • How to write a tsv file in Node? i have to write a column in node using tsv file.pls help me ? – Mariam Oct 14 '19 at 07:14
  • Could you add this in another question? I think that would be better :-). I'm sure we can find a solution to your problem! – Terry Lennox Oct 14 '19 at 07:18
  • 1
    https://stackoverflow.com/questions/58371964/how-can-write-a-column-in-tsv-file-using-node-js pls answer me? – Mariam Oct 14 '19 at 09:15