2

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?

2 Answers2

0

Part of the problem happens because you are not returning the http response. Try this:

Parse.Cloud.define("retrieveToken", (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": "..."
          }
        }
      }
    }
  }).then(function(httpResponse) {
    console.log('Token retrieved successfully, Status Code: ', httpResponse.status, ',\n', httpResponse.text);
    return httpResponse;
  }, function(httpResponse) {
    console.error('Failed to retrieve token, retrieveToken: ', httpResponse.status, ',\n', httpResponse.text);
    return httpResponse;
  });
});
Davi Macêdo
  • 2,954
  • 1
  • 8
  • 11
  • Hi Davi, actually I just want to view it using console.log first, and I'm going to store the token in my database next. So there is no need to return the httpResponse. Anyway, thank you for your answer XD – Phoebe Tsui Sep 15 '21 at 17:24
0

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