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


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:

    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
                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");

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

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 :


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