# Issue in getting response from http call

**URL:** <https://discourse.nodered.org/t/issue-in-getting-response-from-http-call/42291>\
**Category:** General\
**Created:** [9 March 2021 00:20 UTC](https://discourse.nodered.org/t/issue-in-getting-response-from-http-call/42291 "2021-03-09T00:20:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![arvindharitus](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@arvindharitus](https://discourse.nodered.org/u/arvindharitus)\
**Post date:** [9 March 2021 00:20 UTC](https://discourse.nodered.org/t/issue-in-getting-response-from-http-call/42291/1 "2021-03-09T00:20:27Z")

</div>

Good morning,

This is my first assignment of Node-Red and I am creating a custom-node that will get data from an API. To get the data I need to send an access token from another host.

I am getting the access token through postman

 ![Postman](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/3/b39417a075a77e22f2e608b05d5cb89781e8814a.jpeg)

but when I tried it through code it gives me no response (even if I pass the incorrect ClientSecret).

See my code below...

> ```auto
> node.on('input', function(msg) {
> var http = require('http');
> 
> ```

```
  var options = {
    host: 'hostname'
    path: '/master/protocol/openid-connect/token',
    headers: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    data: {
      grant_type:'client_credentials',
      client_id:'api',
      client_secret:'7 ********* -ebeb-472b-a61c-cb88959f682e',
      audience:'API_IDENTIFIER'       
    },
    method: 'POST'
  };

  callback = function(response) {
    var str = ''  
    response.on('data', function (chunk) {
      str += chunk;
      msg.payload = String(str); 
    });
    response.on('end', function () {
      console.log(str);
    });
  }

  var req = http.request(options, callback);

  req.on('error', function(e) {
    console.log('Error with the request:', e);
  });

  req.end();
 }

  node.send(msg);
});

```

> ```auto
> 
> ```

My Output when running the flow...

 ![Output](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/7/17b88433340b54be72ab3aaeeff5d51f993bd8fc.jpeg)

Please suggest.

---

<div class="post-metadata">

**Author:** ![arvindharitus](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@arvindharitus](https://discourse.nodered.org/u/arvindharitus)\
**Post date:** [10 March 2021 03:22 UTC](https://discourse.nodered.org/t/issue-in-getting-response-from-http-call/42291/2 "2021-03-10T03:22:52Z")

</div>

Solution:

Need to Stringify the body with the help of 'qs'.and did it with the help of node-fetch

```auto
const data = qs.stringify({
            grant_type: 'client_credentials',
            client_id: 'apicall',
            client_secret: ' ************-************* - ************ cb88959f682e'
        });
 
        const headers = {
            'content-type': 'application/x-www-form-urlencoded'
        }
        let token = "";

        var fetch = require("node-fetch");
        let url = 'https:// *********************************** /openid-connect/token';
        fetch(url, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
          },
          body: data
        }).then(res => res.json().then(data => console.log(data.access_token)));

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [24 March 2021 03:23 UTC](https://discourse.nodered.org/t/issue-in-getting-response-from-http-call/42291/3 "2021-03-24T03:23:51Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
