Search code examples
javascriptparse-platformhttprequestparse-cloud-codeback4app

Parse.Cloud.httpRequest returns 'undefined' instead of a response using POST method


I'm making a Parse.Cloud.httpRequest using the POST method to retrieve a token from an API in a Back4App Cloud Code Function called 'retrieveToken'.

Parse.Cloud.define("retrieveToken", async(request) => {
  Parse.Cloud.httpRequest({
    method: 'POST',
    url: 'https://.......',
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    },
    body: {
      "auth": {
        "identity": {
          "methods": [
            "password"
          ],
          "password": {
            "user": {
              "name": "...",
              "password": "...",
              "domain": {
                "name": "..."
              }
            }
          }
        },
        "scope": {
          "project": {
            "id": "..."
          }
        }
      }
    }
  }).then(function(httpResponse) {
    console.log('Token retrieved successfully, Status Code: ', httpResponse.status, ',\n', httpResponse.text);
  }, function(httpResponse) {
    console.error('Failed to retrieve token, retrieveToken: ', httpResponse.status, ',\n', httpResponse.text);
  });
});

It works fine if I delete "project": {"id": "..."} from the 'scope' of the body JSON of the httpRequest.

However, it returns me 'undefined' if I don't delete that chunk of code.

I have to keep that chunk of code there because the API will return me a project-level token only if I specify the project id. Otherwise, I will get a global-level token, which cannot be used for my case.

The API works perfectly with/without the project id specified in Postman.

I have already stuck here for a whole day and still can't figure it out. Where am I doing wrong and how can I get the correct response from Parse.Cloud.httpRequest with the project id specified?

Update:

I found the content-length for the responses are quite different with/without the project id specified using Postman.

'content-length' = 6752 Bytes without the project id specified

'content-length' = 16896 Bytes with the project id specified

By any chance the 'undefined' was caused by the response size limit? If this is the reason, how can I get the correct response? Can I receive the header only?


Solution

  • This issue was resolved by adding an environment variable to my Back4App server.

    NODE_OPTIONS="--max-http-header-size=16384"
    

    I knew the exact problem (header overflow) by using the code below:

    Parse.Cloud.define("retrieveToken", async(request) => {
      return Parse.Cloud.httpRequest({
        method: 'POST',
        url: 'https://.......',
        headers: {
          'Content-Type': 'application/json;charset=utf-8'
        },
        body: {
          "auth": {
            "identity": {
              "methods": [
                "password"
              ],
              "password": {
                "user": {
                  "name": "...",
                  "password": "...",
                  "domain": {
                    "name": "..."
                  }
                }
              }
            },
            "scope": {
              "project": {
                "id": "..."
              }
            }
          }
        }
      });
    });
    

    Only the code above returned me the error below, and all other code returned me 'undefined':

    [Error]: Parse Error: Header overflow (Code: 141, Version: 1.19.2)
    

    FYI: I'm calling this API

    https://iam.ap-southeast-1.myhuaweicloud.com/v3/auth/tokens