制作Parse.Cloud.httpRequest时的400状态代码

时间:2015-09-15 07:19:47

标签: parse-platform cloud-code

我收到了400错误,这可能意味着由于语法错误导致格式错误的请求。然而,我看不到它,所以也许别人可以发现它。这是代码:

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

变量userIdlayerProviderID是正确的字符串,是有效的ID。 URL是正确的,授权令牌也是正确的。

仅供参考,它使用名为Layer的消息传递平台向新注册的用户发送欢迎消息。 Layer有一个Platform API,它允许各种功能,比如向用户发送消息或发送通知(这就是我在这里做的)。

我已根据API要求检查了body;这是Layer的样本:

{
    "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"
    }
}

所以,正如我所说,我被困住了。任何帮助都感激不尽。

提前致谢!

1 个答案:

答案 0 :(得分:2)

是否有助于将您的网址从https:api.layer.com/apps/更改为https://api.layer.com/apps/