G KAMESWAR RAO
G KAMESWAR RAO

Reputation: 41

Is there way to specify local file system path in nodeJS POST API request , am able to make api call using curl but not with nodejs

Following curl API is successfully deploying .zip file from the local file system into the Azure Function APP.

curl -X POST -u user123:P@ssword --data-binary @"C:\Ddrive\Porject\deploy-zip\wb-uc-code.zip" "https://abc-world.scm.azurewebsites.net/api/zipdeploy"

But I wanna achieve the same with NodeJs: So I converted it as -

function () {

var dataString = "@C:\Ddrive\Workbench\deploy-zip\wb-uc1.zip";

var options = {
    url: 'https://abc-world.scm.azurewebsites.net/api/zipdeploy',
    method: 'POST',
    body: dataString,
    auth: {
        'user': 'user123',
        'pass': 'P@ssword'
    }
};

request.post(options, (response, error) => {
    if (error) {
        console.log("error");
    }
    else {
        console.log(response.body);
    }
})

}

while executing am getting error: enter image description here ------->>> Most Probably I think am unable to provide file-path appropriately in Options. Can someone help with this?

Upvotes: 1

Views: 382

Answers (1)

Tony Ju
Tony Ju

Reputation: 15629

There are two things you need to pay attention to.

1.You should pass data-binary, you were passing path string in your code.

2.The order of response and error is reversed.

Please refer to the working code as below.

var request=require('request')
var fs = require("fs")

var dataString=fs.createReadStream("D:\\testProject\\NodeJs\\nodejs-docs-hello-world\\test4.zip");

var options = {
    url: 'https://tonytestwebnode.scm.azurewebsites.net/api/zipdeploy',
    method: 'POST',
    body: dataString,
    auth: {
        'user': 'tonytestweb',
        'pass': 'XXXX!'
    }
};

request.post(options, (error, response) => {
    console.log(response.statusCode);
})

Upvotes: 1

Related Questions