Basylio
Basylio

Reputation: 96

Firebase Cloud Functions response error iOS

I want to use Firebase Cloud Functions so I started with the simple "Hello world" example as a backend part and with the iOS example of calling functions right from the app.

Cloud Functions:

export const helloWorld = functions.https.onRequest((request, response) => {
    response.send('{"response":"Hello world"}') //option3
    response.send('Hello world');//option2
    response.send("Hello world");//option1 as in docs
});

I've tried 3 different options of the response. The console says it works. If open function url in browser it prints "Hello world".

iOS part:

[[_functions HTTPSCallableWithName:@"helloWorld"] callWithObject:nil
  completion:^(FIRHTTPSCallableResult * _Nullable result, NSError * _Nullable error) {
      if (error) {
          if (error.domain == FIRFunctionsErrorDomain) {
              NSLog(@"domain code %ld@, details %@", error.code, error.userInfo[FIRFunctionsErrorDetailsKey] );
          }
          NSLog(@"code %ld, message %@, details %@", error.code,error.localizedDescription, error.userInfo[FIRFunctionsErrorDetailsKey]);
          return;
      }
      NSLog(@"result: %@", result.data);
 }];

It returns (in each of 3 options): code 3840, message The data couldn’t be read because it isn’t in the correct format., details (null)

What can I do with the response format if it's handled all the way by Firebase?

Upvotes: 0

Views: 1092

Answers (2)

coolcool1994
coolcool1994

Reputation: 3812

For success, you need to send the data back in dictionary with data key format. This is true at least in iOS. In browser, you can send it back in any format.

For example:

response.send({ data = {"response":"Hello world"}})

Upvotes: 0

pulyaevskiy
pulyaevskiy

Reputation: 297

In your function code you create regular HTTPS trigger function (https.onRequest), for callables you need to use https.onCall instead.

The benefit of using Callables is that they handle authorization part for you (for regular HTTPS triggers you'll need to write your own code to authenticate users).

The downside of Callables is that they must follow specific protocol. Though should still be able to return any JSON-serializable data from Callables.

If you don't need authentication you can use regular HTTPS triggers, and you can simply send regular HTTP requests without using an SDK.

Read more about callables: https://firebase.google.com/docs/functions/callable

Upvotes: 0

Related Questions