2016-09-21 316 views
1

我想向kaa服务器发送通知。下面的cURL命令工作正常,但我想从我的node.js服务器发送POST请求。请帮助我转换为发布请求。将cURL命令转换为向kaa服务器发送通知的请求

curl -v -S -u devuser:devuser123 -F'notification= 
{"applicationId":"32769","schemaId":"32778","topicId":"32770","type":"USER"}; 
type=application/json' -F [email protected] "http://localhost:8080/kaaAdmin/rest/api/sendNotification" | python -mjson.tool 

我想是这样的:

var notificationValue= {"applicationId":"32769","schemaId":"32778","topicId":"32770","type":"USER"}; 
var file = 'notification.json'; 
var opts = { 
    url: 'http://localhost:8080/kaaAdmin/rest/api/sendNotification', 
    method: 'POST', 
    auth: { user: 'devuser', password: 'devuser123' }, 
    json: true, 
    formData: { 
      notification: JSON.stringify(notificationValue), 
      file : fs.readFileSync(file) 
    } 

}; 
request(opts, function(err, resp, body) { 
    if(err) 
     res.send(err); 
    else{ 
     res.send(body); 
    } 
}); 

我得到错误:400需要请求部分 '通知' 是不存在的。

回答

3

这是一个解决方案。

首先导入下一个模块。

var fs = require('fs'); 
var request = require('request'); 
var crypto = require('crypto'); 

我们需要两个实用的功能来生成多内容类型和其他建设原料POST请求主体边界

var CRLF = "\r\n"; 
var md5 = crypto.createHash('md5'); 

function multipartRequestBodyBuilder(fields, boundary) { 
    var requestBody = ''; 
    for(var name in fields) { 
     var field = fields[name]; 
     var data = field.data; 
     var fileName = field.fileName ? '; filename="' + field.fileName + '"' : ''; 
     var type = field.type ? 'Content-Type:' + field.type + CRLF : ''; 
     requestBody += "--" + boundary + CRLF + 
       "Content-Disposition: form-data; name=\"" + name + "\"" + fileName + CRLF + 
       type + CRLF + 
       data + CRLF; 
    } 
    requestBody += '--' + boundary + '--' + CRLF 
    return requestBody; 
} 

function getBoundary() { 
    md5.update(new Date() + getRandomArbitrary(1, 65536)); 
    return md5.digest('hex'); 
} 

function getRandomArbitrary(min, max) { 
    return Math.random() * (max - min) + min; 
} 

然后我们形成我们的数据并生成边界。

var notificationValue = { 
    "applicationId":"2", 
    "schemaId":"12", 
    "topicId":"1", 
    "type":"USER" 
}; 


var postData = { 
    notification : { 
     data : JSON.stringify(notificationValue), 
     type : "application/json" 
    }, 
    file : { 
     data : fs.readFileSync("message.json"), 
     fileName : 'notification.json', 
     type : 'application/octet-stream' 
    } 
} 


var boundary = getBoundary(); 

之后,撰写请求并发送到Kaa服务器。

var opts = { 
    url: 'http://localhost:8080/kaaAdmin/rest/api/sendNotification', 
    method: 'POST', 
    auth: { user: 'devuser', password: 'devuser123' }, 
    headers: { 
    'content-type': 'multipart/form-data; boundary=' + boundary 
    }, 
    body : multipartRequestBodyBuilder(postData, boundary) 
}; 

request(opts, function(err, resp, body) { 
    if(err) { 
     console.log("Error: " + err); 
    } else { 
     console.log("Satus code: " + resp.statusCode + "\n"); 
     console.log("Result: " + body); 
    } 
}); 

毕竟,你会看到状态码200

Status code: 200 

Result: { 
    "id" : "57e42623c3fabb0799bb3279", 
    "applicationId" : "2", 
    "schemaId" : "12", 
    "topicId" : "1", 
    "nfVersion" : 2, 
    "lastTimeModify" : 1474569763797, 
    "type" : "USER", 
    "body" : "CkhlbGxvAA==", 
    "expiredAt" : 1475174563793, 
    "secNum" : 17 
} 

我附上与我的通知演示测试从卡阿沙箱整个代码文件的确认响应:send notification

+0

你太棒了。非常感谢。 –