ios 如何从云函数中的请求中获取参数?火力基地
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45018919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get parameters from request in cloud functions? Firebase
提问by Alexander Khitev
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?
我制作了 https 云功能,并使用 iOS 应用程序向它发出请求。调用了 Cloud 函数,因为我可以看到日志,但是我无法从我从 iOS 应用程序发送的请求中获取参数。我尝试了几种不同的选择,但它们对我有帮助,请告诉我如何实现结果?
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
iOS应用代码
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");
});
回答by Alexander Khitev
I read this postand 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");
});
回答by Deepak Sharma
For Web: (POST Request)
对于 Web:(POST 请求)
We have:
我们有:
export const myFunction = functions.https.onRequest((req, res) => {
const conversationID = req.body.conversationID;
const currentUserID = req.body.currentUserID;
return ;
});
回答by coolcool1994
[[[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.
iOS 将数据放入 request.body.data.key 中,因此如果您正在查询中查找从 iOS 传入的数据,那么您正在查看错误的位置。当您在 url 中使用 ?key=value 参数从浏览器调用该函数时, request.query.key 具有数据。但是当从应用程序调用时,传递的数据在 request.body.data 对象中 - 我检查了 iOS SDK 的源代码,是的,这就是它的实现方式。