Home > front end >  Requests to API from LocalHost NodeJS/Express app fail, but work in PostMan?
Requests to API from LocalHost NodeJS/Express app fail, but work in PostMan?

Time:12-28

Here is the code I am using now and plan to have become middleware after I can request back the token from their API for requests.

exports.requestToken = function (req, res) {
  const connectionInfo = {
    params: {
      whitelabel_id: crowdContentAuth.api_username,
      whitelabel_key: crowdContentAuth.api_password,
      client_id: crowdContentAuth.client_id,
      client_key: crowdContentAuth.client_key,
    },
  };
  const options = {
    hostname: "api.crowdcontent.com/?request="   JSON.stringify(connectionInfo),
    port: 443,
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
  };
  http.request(options, (res) => {
    console.log(res);
  });
};

When I request from my application I see the following in the console:

0|Server |   errno: 'ENOTFOUND',
0|Server |   code: 'ENOTFOUND',
0|Server |   syscall: 'getaddrinfo',
0|Server |   hostname: 'api.crowdcontent.com/?request={"params":{"whitelabel_id":"redacted","whitelabel_key":"redacted","client_id":"redacted","client_key":"redacted"}}'

I am redacting things in the response obviously because it's not-public data, but it's there. The CrowdContent API is also a bit older and not exactly how you would typically expect a POST based API to work since the data should be passed in the body of the request and not via the URL.

The API states to use https://DOMAIN/request= however after testing in PostMan it appears to be https://api.crowdcontent.com/?request={stringifiedObjectOfDetails - When I send a request this way through PostMan I do get a successful response with a token.

{"message":"Token is already created. Add the token to your API call are you are good to go.","token":"5713a4d4100b2redacting41e76b4efef0","last_access":"blah"}

Is this something I am doing wrong with my request or is this possibly CloudFlare getting involved with the request since it's coming from my localhost/ip?

Clueless at this point to figure out what's causing it.

All other APIs and Post requests in my app are working.

CodePudding user response:

i think you should you other packages like axios, node-fetch than using http built-in module instead.

  • Related