javascript - 无法发送异步 POST 请求

标签 javascript node.js typescript async-await

我正在尝试等待发帖请求。我找到了 request-promise-native 包来发出等待请求。它适用于 GET 请求,但不适用于 POST。 URL 有效,身份验证哈希有效,并且我已经使用“curl”对其进行了测试。

import * as request from "request-promise-native";

async sendRequest(uri: string, method: string): Promise<any> {
    var options = {
        uri: uri,
        headers: {
            "Authorization": 'Basic ' + 'someValidHashValue'
        },
        method: method,
        json: true
    };

    try {
        const result = await request.get(options);
        return result;
    }
    catch (err) {

        console.log(err);
    }
}

async queueBambooPlan(fileName: string) {
    let bambooHost: string | undefined = vscode.workspace.getConfiguration('markdown-table-of-contents').get('atlassianBambooHost');
    let planKey = await this.getBambooPlanKey(fileName, bambooHost);
    let uri = `${bambooHost}/rest/api/latest/queue/${planKey}`;

    let response = await this.sendRequest(uri, 'post');
}    

405 - "Apache Tomcat/8.0.36 - Error reportH1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}

HTTP Status 405 - Method Not Allowed

type Status report

message Method Not Allowed

description The specified HTTP method is not allowed for the requested resource.

Apache Tomcat/8.0.36

上面的请求使用fiddler工作,但是它在代码中不起作用。

enter image description here

更新:我已经使用标准请求包编写了代码并且它正在工作:

enter image description here

最佳答案

您正在使用 request.get,请改用 request.post,或者仅使用 request(options) 并设置 method 属性。

关于javascript - 无法发送异步 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56243090/

相关文章:

node.js - https请求中承载 token 的用法

javascript - AG-Grid:根据服务器的响应制作可编辑的列

javascript - 在 Vue 3 中使用 reactive 的原语 react 性

android - 为什么在 Google App Engine 中总是只有第一个请求有 11 秒的延迟?

javascript - 使 div 在向下滚动时在 2 个高度之间出现和消失 - - 并在向上滚动时反转

javascript - 如何从所有分片中获取 users.cache 并将它们放入一个集合中,就像 client.user.cache 返回的集合一样?

javascript - Typescript - 动态从字典或数组中获取下一个和上一个项目

typescript - 导出的变量是只读的?

javascript - 使用 javascript 拦截提交后无法 `submit()` html 表单

javascript - 更新名称后,我的数据库 `first_name` 显示为 0,last_name 显示为 "Arora"