0
votes

I'm trying to await post request. I've found request-promise-native package to make await requests. It works for GET requests but doesn't work with the POST. The URL is working, the auth hash is working as well as I've tested it with `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

The request from above worked using fiddler, however it is not working from the code.

enter image description here

UPDATE: I've wrote the code using standard request package and it is working:

enter image description here

1
You can only await inside of an async function... just throwing that out there. So the line await this.sendRequest() is invalid. - Isaac Vidrine
Updated the code - Dmitrij Kultasev
The error message means your server doesn't accept POST requests on that url. - James
Updated the question, the URL is right and it is working using fiddler or curl - Dmitrij Kultasev
Might want to verify that the uri you're using in your code matches that which works in Fiddler. - Chris White

1 Answers

3
votes

You're using request.get, use request.post instead or simply use request(options) with the method property set.