Why is my API call not hitting the endpoint? (Typescript, request-promise)

ⅰ亾dé卋堺 提交于 2021-02-11 13:50:47

问题


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:


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)
    });


来源:https://stackoverflow.com/questions/61280305/why-is-my-api-call-not-hitting-the-endpoint-typescript-request-promise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!