0

I can't get my API call to hit the endpoint. I have 2 TypeScript projects, one is a list of API endpoints, and the other is a process that will call a series of API endpoints to perform operations. The API endpoint will take a JSON Web token and process it in header (Swagger documentation has it defined and brought in as the following:

"security": [
    {
        "Bearer": []
    }
]

where "Bearer" is defined at the security protocols at the top:

"securityDefinitions": {
    "Bearer": {
        "type": "apiKey",
        "name": "Authorization",
        "in": "header"
    }
}

I am using the request-promise package in TypeScript. I send the request and I always get a return object of "undefined". While running the API endpoints on localhost, the breakpoints aren't even getting hit which makes me think it's not even being stepped into.

Code:

const request = require('request-promise');

var options = {
    uri: <endpoint>,
    headers: {
        'User-Agent': 'Request-Promise',
        'encoding': 'utf8',
        'content-type': 'application/json',
        'authorization': `Bearer ${jwt}`
    },
    method: 'GET',
    json: true
};

    request(options)
.then(function (response) {
        console.log(response)
    })
    .catch(function (err) {
        console.error(err)
    });

body of the return is null. Please help me fix this.

1 Answers1

0

Your request return a Promise. In order to capture and use the answer, you must add the following

request(options)
    .then(function (response) {
        console.log(response)
    })
    .catch(function (err) {
        console.error(err)
    });
Raphael Pr
  • 896
  • 10
  • 28
  • Let me clarify: Even with these changes, the issue persists. Is something wrong with my configuration? – NICO BHHC Coder Apr 17 '20 at 21:05
  • That's the thing. It doesn't even scope into either the success or fail paths. It will read the "then(function (response) {" path and the ".catch(function (err) {" path but then just skip to the end. The log reads as if the code wasn't even run! – NICO BHHC Coder Apr 17 '20 at 21:10
  • Have you try to curl the request, to exclude the case were where the server is not answering. You can also try adding a timeout to your request (timeout: 15000 for instance). – Raphael Pr Apr 17 '20 at 21:17