Reputation: 1461
I'm getting a 400 error returned, which presumably means a malformed request due to a syntax error. I can't see it, however, so perhaps someone else can spot it. Here's the code:
Parse.Cloud.define("sendWelcome", function(req, res)
{
var userId = req.params.userId;
var welcomeBody =
{
'recipients': [ userId ],
'sender': { 'name': 'RealSheek' },
'parts':
[{
'body': 'Welcome to RealSheek!\n\nThanks for becoming a member. We hope you have fun with RealSheek - finding fun stuff, sharing it with your friends as you chat with them about it, and making collections.\n\nBe sure to invite your friends, and let us know how we can help ("Contact Us" from the app menu).\n\nCheers,\n\nThe RealSheek Team',
'mime_type': 'text/plain'
}],
'notification': { 'text': 'Welcome to RealSheek!', 'sound': 'chime.aiff' }
};
var welcomeBodyJSON = JSON.stringify(welcomeBody);
console.log("\n\n\n"+welcomeBodyJSON);
Parse.Cloud.httpRequest(
{
method: 'POST',
url: "https:api.layer.com/apps/" + layerProviderID + "/announcements",
headers:
{
'Accept' : 'application/vnd.layer+json; version=1.0',
'Authorization' : 'Bearer n4YeGaeJDmsC0kMdem28fsVjNuUOqhO86aqCUYoBNBYzjRP9',
'Content-Type' : 'application/json'
},
body: welcomeBodyJSON
}).then(function(httpResponse)
{
res.success();
},
function(httpResponse)
{
res.error('Request failed with response code ' + httpResponse.status);
});
});
The variables userId
and layerProviderID
are proper strings and are valid IDs. The URL is correct, as is the Authorization token.
FYI, this uses a messaging platform called Layer to send a welcome message to a newly signed-up user. Layer has a Platform API which allows various features like sending messages to users, or sending announcements (which is what I'm doing here).
I've checked the body
against what the API requires; here's Layer's sample:
{
"recipients": [ "1234" ],
"sender": {
"name": "The System"
},
"parts": [
{
"body": "Hello, World!",
"mime_type": "text/plain"
}
],
"notification": {
"text": "This is the alert text to include with the Push Notification.",
"sound": "chime.aiff"
}
}
And so, as I say, I am stuck. Any help gratefully accepted.
Thanks in advance!
Upvotes: 1
Views: 239
Reputation: 691
Would it help changing your url from https:api.layer.com/apps/
to https://api.layer.com/apps/
Upvotes: 2