【问题标题】:Can't send async post request无法发送异步发布请求
【发布时间】:2025-12-13 08:10:02
【问题描述】:

我正在等待发布请求。我找到了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 - 错误 报告H1 {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;} 身体 {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} 乙 {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 {颜色:黑色;}A.name {颜色:黑色;}.line {高度:1px; 背景颜色:#525D76;边框:无;}

HTTP 状态 405 - 方法不允许

类型状态报告

消息 方法不允许

描述指定的 请求的资源不允许使用 HTTP 方法。

Apache Tomcat/8.0.36

上面的请求使用fiddler 工作,但它不能通过代码工作。

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

【问题讨论】:

  • 你只能在异步函数中 await... 把它扔出去。所以await this.sendRequest()这行是无效的。
  • 更新了代码
  • 错误信息表示您的服务器不接受该 url 上的 POST 请求。
  • 更新了问题,网址是正确的,它正在使用 fiddler 或 curl 工作
  • 可能想要验证您在代码中使用的 uri 是否与 Fiddler 中的匹配。

标签: javascript node.js typescript async-await


【解决方案1】:

您正在使用request.get,请改用request.post,或者只是使用request(options)method 属性集。

【讨论】: