fix curl cmd error after debug

This commit is contained in:
shalousun 2021-11-02 20:41:53 +08:00
parent 19506d33f0
commit 627bd476a3
1 changed files with 34 additions and 3 deletions

View File

@ -277,10 +277,12 @@ function toCurl(request) {
if (request.url.indexOf('https') == 0) {
cmd.push("-k");
}
// append Content-Type
if (request.data && request.data.length > 0) {
if (request.contentType) {
cmd.push("-H");
cmd.push("'Content-Type: application/json; charset=utf-8'");
cmd.push("'Content-Type:");
cmd.push(request.contentType+"'");
}
// append request headers
let headerValue;
@ -310,10 +312,39 @@ function toCurl(request) {
}
cmd.push(url);
// append data
if (request.data && request.data.length > 0) {
if (typeof request.data == 'object') {
let index = 0;
const bodyData = [];
bodyData.push("\"")
for (let key in request.data) {
if (Object.prototype.hasOwnProperty.call(request.data, key)) {
if (index===0){
bodyData.push(key);
bodyData.push("=");
bodyData.push(request.data[key]);
} else {
bodyData.push("&")
bodyData.push(key);
bodyData.push("=");
bodyData.push(request.data[key]);
}
}
index++;
}
bodyData.push("\"");
let bodyStr = ""
bodyData.forEach(function (item) {
bodyStr += item;
});
cmd.push("--data");
cmd.push(bodyStr);
} else if (request.data && request.data.length > 0) {
// append json data
cmd.push("--data");
cmd.push("'" + request.data + "'");
}
let curlCmd = "";
cmd.forEach(function (item) {
curlCmd += item + " ";