question

info-9 avatar image
info-9 asked

Cloudscript http.request to Firebase fails

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

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
brendan avatar image
brendan answered

That's correct - the Content Type is specified in the http.request call, and not as a separate header. What is the Title ID for this, and the Revision number of the script, so that we can have a look at the specifics?

3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

info-9 avatar image info-9 commented ·

The title ID is 617E and the script revision number is 13. Thanks for having a look.

0 Likes 0 ·
brendan avatar image brendan info-9 commented ·

Aha! After some experimentation, what I found was that the http library's interpretation of the authorization header is following a strict version of the http spec. But what that means is that if you insert spaces info the actual header value:

    var headers = {
        "Authorization": "key = AAAAi4E..."
    };

It works fine.

1 Like 1 ·
info-9 avatar image info-9 brendan commented ·

Thats great news! Thank you for figuring it out. I tried it as you described it and it works now as it should.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.