How can I get some data from another API in a cloud function?

Hello,

I would like to fetch some data from another API in a Parse.Cloud function, I initially tried using Parse.Cloud.httpRequest inside my function like this:

Parse.Cloud.define(
    "cloudTestFunction",
    async (request) => {
        const query = new Parse.Query("MyParseClass");
        // skipping the lines where I build the query
        const results = await query.find();
        const firstResult = results[0];
        if (firstResult == null) {
            // here I want to get data from another API
            Parse.Cloud.httpRequest({
                url: "otherAPIURL"
            }).then(function(httpResponse) {
                return 'yeah';
            },function(httpResponse) {
                return 'oops';
            });
        } else {
            return 'result found';
            //      return firstResult;
        }
    }
);

When doing this, I get this error:

ā–æ ParseError code=141 error=Parse.Cloud.httpRequest is not a function

  • code : ParseSwift.ParseError.Code.scriptFailed
  • message : ā€œParse.Cloud.httpRequest is not a functionā€
  • otherCode : nil
  • error : nil

Then, I tried using another library to do it, and especially Got.

I ran npm install got, then added const got = require('got'); to the top of my file containing the parse cloud function, and replaced the Parse.Cloud.httpRequest part with this:

const text = await got("otherAPIURL").text();
return text;

But now I get a very similar error:

ā–æ ParseError code=141 error=got(...).text is not a function
    code : ParseSwift.ParseError.Code.scriptFailed
    message : "got(...).text is not a function"
    otherCode : nil
    error : nil

Could you please help me trying to find a solution for this problem.

Thank you for your help

Try wrapping that in parentheses before calling text() on it. Got is returning a Promise which doesnā€™t have text() as a method.

Try const { got } = require(ā€˜gotā€™); instead of const got = require(ā€˜gotā€™);

Hi and thank you for your suggestions, however Iā€™m still unable to get it to work.

When surrounding the await got(apiURL) with parenthesis, I get this error :

{ā€œcodeā€:141,ā€œerrorā€:"(intermediate value).text is not a function"}

I then tried to replace const got = require('got'); with const { got } = require('got');, and this time the error is :

{ā€œcodeā€:141,ā€œerrorā€:ā€œgot is not a functionā€}

Not sure if you need such a solution, but if you want to simply make an HTTP request you can use cross-fetch package. Iā€™m using it without problems.

const fetch = require("cross-fetch");

GET REQUEST
const response = await fetch("your url comes here" );

POST REQUEST
const response = await fetch("your url comes here" , { method: "POST", headers: "your headers come here if exist", body: { //your body comes here if exists } });

1 Like

Thanks for your answer! I tried using cross-fetch and I am now able to get the data! Even better, the HTTP request targets some JSON content, and Iā€™m actually able to get it, here is my code:

Parse.Cloud.define("testHTTP", async (request) => {
  	const response = await fetch("apiURL");
	const json = await response.json();
    return json;
});

When calling the cloud function, I get this result :

{ā€œresultā€:{ā€œresultsā€:[ā€¦],ā€œstatusā€:ā€œOKā€}}

However, I then try this :

return json["result"];

But this results in an empty result :

{}

Iā€™ll try to find the cause, but anyway, thanks a lot again for the advice!

1 Like