问题
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