2016-09-21 2 views
1

Ich möchte eine Benachrichtigung an kaa Server senden. Der folgende cURL-Befehl funktioniert einwandfrei, ich möchte jedoch eine POST-Anfrage von meinem node.js-Server senden. Bitte helfen Sie mir bei der Umstellung auf Post-Anfrage.Convert cURL Befehl zu Post-Anfrage zu senden Benachrichtigung an kaa Server

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 

Ich habe versucht, wie folgt aus:

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); 
    } 
}); 

Ich erhalte: Fehler 400 Teil Erforderliche Anfrage 'Mitteilung' nicht vorhanden ist.

Antwort

3

Hier ist eine Lösung.

Zuerst die nächsten Module importieren.

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

Wir brauchen zwei Nutzenfunktionen Grenze für multiInhaltsTyp zu erzeugen, und das andere Ausgangs POST-Anfrage Körper zu bauen.

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; 
} 

Dann bilden wir unsere Daten und die Grenze zu erzeugen.

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(); 

Danach erstellen Sie eine Anfrage und senden an Kaa Server.

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); 
    } 
}); 

Immerhin erhalten Sie die Bestätigung Antwort mit dem Statuscode siehe 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 
} 

ich die Datei mit ganzen Code anhängen, die ich auf dem Notification Demo von Kaa Sandbox getestet: send notification.

+0

Sie sind fantastisch. Ich danke dir sehr. –