【问题标题】:Why are the parameters i'm passing to the server with Alamofire undefined?为什么我在未定义 Alamofire 的情况下传递给服务器的参数?
【发布时间】:2018-03-09 19:15:03
【问题描述】:

我有一个使用 alamofire 从 swift 到 google cloud functions/node.js 的 http 请求:

 Alamofire.request(url, method: .post, parameters: [
            "api_version": apiVersion,
            ])

我知道请求正在访问服务器并在服务器上执行函数,因为我打印出一条日志消息,表明我在函数中。

我也知道客户端的 apiVersion 包含的数据是条带 api 版本。

但是在这个函数中的服务器上:

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

console.log("activated ephemeral keys") // prints to the console

const stripe_version = req.query.api_version //is undefined

console.log(stripe_version) 未定义日志记录。

有什么想法吗?

******更新*********

我看了这篇文章,我怀疑存在编码问题,但建议会很好。

How to add Alamofire URL parameters

谢谢。

【问题讨论】:

  • 如果我没看错,您在 POST 正文中发送参数,但您需要查询参数。
  • 因此,请尝试将编码参数添加到您提供的链接中提到的 Alamofire 的 request 函数。
  • 我找到的答案是将节点中服务器上的行 req.query.api_version 更改为 req.body.api_version。不过谢谢。
  • 是的,您要么更改 iOS 应用程序,要么更改服务器行为。

标签: swift alamofire google-cloud-functions


【解决方案1】:

这行得通吗?

Alamofire.request(url, 
                  method: .post, 
                  parameters: ["api_version": apiVersion], 
                  encoding: URLEncoding(destination: .queryString))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 2016-03-07
    • 2020-05-16
    • 1970-01-01
    • 2020-10-16
    • 2011-04-30
    相关资源
    最近更新 更多