I'm trying to send a http.request via cloudscript to Firebase but it returns an error. It does works with Postman.
My cloudscript is this:
handlers.SendPushAnd = function (args) { var headers = { // "Content-Type": "application/json", "Authorization": "key=AAAAi4E..." }; var notif = { body: "mytestmessage" }; var contentobj = { to: "dPuqPa7-We...", notification: notif }; var url = "https://fcm.googleapis.com/fcm/send"; var content = JSON.stringify(contentobj); var httpMethod = "POST"; var contentType = "application/json"; var response = http.request(url, httpMethod, content, contentType, headers); return response; };
and the returned result is
{ "code": 200, "status": "OK", "data": { "FunctionName": "SendPushAnd", "Revision": 13, "Logs": [ { "Level": "Error", "Message": "HTTP request error", "Data": { "url": "https://fcm.googleapis.com/fcm/send", "method": "POST", "content": "{\"to\":\"dPuqPa7-We...\",\"notification\":{\"body\":\"mytestmessage\"}}", "contentType": "application/json", "headers": { "Authorization": "key=AAAA..." }, "result": { "responseContent": null, "httpStatus": null, "httpStatusCode": 0, "requestError": "InternalError" }, "httpRequestError": "InternalError" } } ], "ExecutionTimeSeconds": 0.0088671, "ProcessorTimeSeconds": 0.016, "MemoryConsumedBytes": 129624, "APIRequestsIssued": 0, "HttpRequestsIssued": 1, "Error": { "Error": "CloudScriptHTTPRequestError", "Message": "The script made an external HTTP request, which returned an error. See the Error logs for details.", "StackTrace": "Error\n at handlers.SendPushAnd (617E-main.js:38:25)" } }, "CallBackTimeMS": 668 }
One thing that might be the problem is that the Firebase docs state that they want two headers. The "Authorization" header as I have it in my cloudscript. And the "Content-Type" header which I have commented out in my cloudscript, because @Brendan stated here and here and here that we should leave it away. Anyway I tried both ways: With the "Content-Type" header commented in and commented out, but the error result is the same in both cases.
When I put the same parameters to Postman then the result is succesful as follows (though via Postman it only works when the "Content-Type" is specified as header):
Status: 200 OK { "multicast_id": 7093457573933731788, "success": 1, "failure": 0, "canonical_ids": 0, "results": [ { "message_id": "0:1502376164613082%1f648b831f648b83" } ] }