Reputation: 471
Who knows what did I wrong in implementing this on a Node.js server?
The parameter were valid and it worked with the Poster on my local Mac. Node.js and MFP 8 Beta are running locally on the Mac.
Here is the code for the server.js file and the steps are:
write the json Push Data
app.post('/award', function(req, res){
var notificationMessage = req.body.message;
// prepare the header
// MFP Settings
var theAuthorization = "Bearer eyJhbGciOiJSUzI1NiIsImp…….Wg";
var appname = 'com.ionicframework.checkapp';
var http = require('http');
var theHost = 'localhost'; // here only the domain name
var thePort = 9080;
var thePath = 'imfpush/v1/apps/' + appname + '/messages';
var theMethode = 'POST';
var postheaders = {
'Authorization' : theAuthorization ,
'Content-Type' : 'application/json'
};
// the post options
var optionspost = {
host : theHost,
port : thePort,
path : thePath,
method : theMethode,
headers : postheaders
};
// create the JSON object for MFP Push
var jsonObject = JSON.stringify({"message":{"alert" :notificationMessage}});
console.info('---> Options prepared:');
console.info(optionspost);
console.info('---> Do the POST call');
// do the POST call using http
var reqPost = http.request(optionspost, function(res) {
console.log("---> statusCode: ", res.statusCode);
console.log("---> headers: ", res.headers);
res.on('data', function(d) {
console.info('---> POST result:\n');
process.stdout.write(d);
console.info('\n\n---> POST completed');
});
});
// write the json Push Data
reqPost.write(jsonObject);
reqPost.end();
reqPost.on('error', function(e) {
console.error(e);
});
res.end("OK");
});
I get the statusCode:400 and this is console output:
Options prepared:
{ host: 'localhost',
port: 9080,
path: 'imfpush/v1/apps/com.ionicframework.checkapp/messages',
method: 'POST',
headers:
{ 'Content-Type': 'application/json',
Authorization: 'Bearer eyJhbGciOiJSUzI1NiIsImp3ayI6......DjbgjqVz5JFVcT8i5k_JWg' } }
---> Do the POST call
---> statusCode: 400
---> headers: { 'content-length': '0',
connection: 'Close',
date: 'Wed, 22 Jun 2016 12:02:50 GMT' }
These were my information sources: https://isolasoftware.it/2012/05/28/call-rest-api-with-node-js/ and https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8.0/notifications/sending-push-notifications/
Upvotes: 1
Views: 2058
Reputation: 471
Thanks @Idan for the text validation and @Nathan for the comment.
I found the problem and now it works. I changed the order of the request preparation and some changes in the code.
Code changes:
Upvotes: 1