28

I made https cloud functions, and I make a request to it with the iOS app. The Cloud function is called, since I can see the log, but I can not get the parameters from the request that I sent from the iOS app. I tried several different options but they helped me, please tell me how I can achieve the result?

const functions = require('firebase-functions');
const admin = require('firebase-admin');

module.exports = functions.https.onRequest((req, res) => {
    console.log("getChatGroupConversationAvatars start", req.params);
    const requestDict = req.params;
    console.log("requestDict", requestDict);
    const queryFormat = req.query.format;
    console.log("queryFormat", queryFormat);
    const conversationID = requestDict["conversationID"];
    const currentUserID = requestDict["currentUserID"];

    console.log("conversationID", conversationID, "currentUserID", currentUserID);

    return console.log("getChatGroupConversationAvatars");
});

iOS app code

class ChatAvatarsManager {

    func getChatGroupConversationAvatars(_ conversationID: String) {
        DispatchQueue.main.async {
            guard let currentUserID = RealmManager().getCurrentUser()?.id else { return }
            DispatchQueue.global(qos: .background).async {
                let path = "https://us-central1-exampleapp.cloudfunctions.net/getChatGroupConversationAvatars"
                guard let url = URL(string: path) else { return }
                let parameters = ["conversationID" : conversationID, "currentUserID" : currentUserID]
                AlamofireExampleManager.shared.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
                    switch response.result {
                    case .success:
                        debugPrint("getChatGroupConversationAvatars success")
                    case .failure(let error):
                        if error._code == NSURLErrorTimedOut {
                            //timeout here
                            debugPrint("getChatGroupConversationAvatars timeOut")
                        }
                        debugPrint("getChatGroupConversationAvatars \(error)")
                    }
                }
            }
        }
    }

}

struct AlamofireExampleManager {

    static let shared: SessionManager = {
        let configuration = URLSessionConfiguration.default
        configuration.timeoutIntervalForRequest = 10
        let sessionManager = Alamofire.SessionManager(configuration: configuration)
        return sessionManager
    }()

For example I got this result in logs

conversationID undefined currentUserID undefined

I also tried it

module.exports = functions.https.onRequest((req, res) => {

    const conversationID = req.params.conversationID;
    const currentUserID = req.params.currentUserID;

    console.log("conversationID", conversationID, "currentUserID", currentUserID);

    return console.log("getChatGroupConversationAvatars");
});
Alexander Khitev
  • 6,417
  • 13
  • 59
  • 115

3 Answers3

66

I read this post and rewrote my code so it works for me.

const functions = require('firebase-functions');
const admin = require('firebase-admin');

module.exports = functions.https.onRequest((req, res) => {

    const conversationID = req.query.conversationID;
    const currentUserID = req.query.currentUserID;

    console.log("conversationID", conversationID, "currentUserID", currentUserID);

    return console.log("getChatGroupConversationAvatars");
});
Alexander Khitev
  • 6,417
  • 13
  • 59
  • 115
8

For Web: (POST Request)

We have:

export const myFunction = functions.https.onRequest((req, res) => {

    const conversationID = req.body.conversationID;
    const currentUserID = req.body.currentUserID;

 return ;
});

Ref : Firestore Read From Values

Deepak Sharma
  • 1,401
  • 19
  • 21
  • 6
    Thanks this helped! Also note that calling a httpsCallable by default puts params in a json object named data so I had to do req.body.data.conversationID – Jerry Sha Jul 11 '18 at 02:05
0
[[[FIRFunctions functions] HTTPSCallableWithName:@"functionName"] callWithObject:@{@"key": value}
                                                              completion:^(FIRHTTPSCallableResult * _Nullable result, NSError * _Nullable error) { 

iOS puts the data into request.body.data.key so if you are looking for passed in data from iOS in query you are looking at a wrong place buddy. When you call the function from the browser using ?key=value param in url, request.query.key has the data. But when called from the app, the passed data is in request.body.data object - I checked the source code from iOS SDK and yeah that's how it is implemented.

coolcool1994
  • 3,704
  • 4
  • 39
  • 43