question

marius avatar image
marius asked

Unable to send http.request to firebaseio database from cloud script

This is my cloud script code:

handlers.testFirebase = function(args) {
    var bt = fbt();
      if (bt == null) return false;
          
    var headers = {
          "Authorization": "Bearer " + bt
    };
    
    var body = {
          LastUpdated: new Date().toISOString()
    };

    var url = "https://xxxxx.firebaseio.com/users/5C731FBCA23062D3.json";
    var content = JSON.stringify(body);
    var httpMethod = "PATCH";
    var contentType = "application/json";
    var logRequestAndResponse = true;


    var response = http.request(url, httpMethod, content, contentType, headers, logRequestAndResponse);
  
    return response;
}

This in return gives me the following output from Postman:

{
  "code": 200,
  "status": "OK",
  "data": {
    "FunctionName": "testFirebase",
    "Revision": 164,
    "Logs": [
      {
        "Level": "Error",
        "Message": "HTTP request error",
        "Data": {
          "url": "https://xxxxx.firebaseio.com/users/5C731FBCA23062D3.json",
          "method": "PATCH",
          "content": "{}",
          "contentType": "application/json",
          "headers": {
            "Authorization": "Bearer <token>"
          },
          "result": {
            "responseContent": null,
            "httpStatus": null,
            "httpStatusCode": 0,
            "requestError": "InternalError"
          },
          "httpRequestError": "InternalError"
        }
      }
    ],
    "ExecutionTimeSeconds": 0.0898891,
    "ProcessorTimeSeconds": 0,
    "MemoryConsumedBytes": 150920,
    "APIRequestsIssued": 1,
    "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.testFirebase (8C10-main.js:626:22)"
    }
  }
}

What is "InternalError" and how can i view them?

CloudScript
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.

marius avatar image marius commented ·

Sorry, content should be as follows, i pasted from wrong result

"content": "{\"LastUpdated\":\"2016-09-18T18:39:44.322Z\"}",
0 Likes 0 ·
marius avatar image marius commented ·

After more testing it seems like "PATCH" is not supported by http.request

When i tried "PUT" this worked, but its not optimal. Is there any chance you can add PATCH,or why PATCH is not supported?

0 Likes 0 ·
brendan avatar image brendan marius commented ·

Correct - we initially shipped Cloud Script with support for the original REST verbs, as shown below. I'm proposing adding "patch" - we'll review and update here this week.

0 Likes 0 ·

1 Answer

·
brendan avatar image
brendan answered

There are two problems: The first is the method you have set - "patch". The Cloud Script http call currently supports "get", "post", "put", and "delete". I'm proposing that we add "patch" as a supported verb now, as it looks like it's really required for efficient use of Firebase.

The second is that while there is a sixth parameter on the request call, and it does happen to be a bool, it's not logRequestandResponse - you set that on the call to ExecuteCloudScript, and it applies to all calls in that script execution pass. The sixth parameter on request is actually for our internal use only - it tells the system to use our test proxy, which isn't valid for live title use.

2 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.

marius avatar image marius commented ·

Thanks for your answer.

I got the example from here

https://github.com/PlayFab/CloudScriptSamples/blob/master/BasicSample/basic_sample.js

Maybe it needs to be updated or specified better regarding the last parameter.

0 Likes 0 ·
brendan avatar image brendan marius commented ·

Thanks! That must have been from when the script was being tested internally, then. I've corrected it.

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.