为什么我的 API 调用没有到达端点?(打字稿,请求-承诺) [英] Why is my API call not hitting the endpoint? (Typescript, request-promise)

查看:19
本文介绍了为什么我的 API 调用没有到达端点?(打字稿,请求-承诺)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 API 调用无法到达端点.我有2个TypeScript项目,一个是API端点列表,另一个是会调用一系列API端点来执行操作的进程.API 端点将采用 JSON Web 令牌并在标头中对其进行处理(Swagger 文档对其进行了定义并引入如下:

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": []
    }
]

在顶部的安全协议中定义了Bearer":

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

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

我在 TypeScript 中使用 request-promise 包.我发送请求,我总是得到一个未定义"的返回对象.在本地主机上运行 API 端点时,断点甚至没有被命中,这让我觉得它甚至没有被踩到.

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.

代码:

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.

推荐答案

您的请求返回一个 Promise.为了捕获和使用答案,您必须添加以下内容

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

这篇关于为什么我的 API 调用没有到达端点?(打字稿,请求-承诺)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆