2015-09-15 45 views
1

我收到400錯誤返回,這可能意味着由於語法錯誤造成格式錯誤的請求。然而,我看不到它,所以也許別人可以發現它。下面的代碼:400使用Parse.Cloud.httpRequest時的狀態代碼

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是正確的字符串,並有效身份證件。該URL與授權令牌一樣正確。僅供參考,它使用稱爲Layer的消息傳遞平臺向新註冊的用戶發送歡迎消息。 Layer有一個平臺API,允許向用戶發送消息或發送公告等各種功能(這就是我在這裏所做的)。

我檢查了body針對API的要求;這裏的圖層樣本:

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

缺少斜槓https://開頭 – Faizan

+0

輝煌!是的,謝謝 - 這是問題所在。如果您已將此發佈爲「答覆」而非評論,那麼我可以給您正確答案的功勞。無論如何,謝謝 - 感謝您花時間看這個! – Lew

回答

2

它是否有助於改變你的網址從https:api.layer.com/apps/https://api.layer.com/apps/

+0

是的。你和上面的@Faizan抓住了這一點。謝謝。 – Lew