【问题标题】:What is the parameter argument used for in Alamofire request? I got results without specifying parametersAlamofire 请求中使用的参数参数是什么?我在没有指定参数的情况下得到了结果
【发布时间】:2020-09-16 13:43:14
【问题描述】:

我使用 Alamofire 在 Swift 中联网,并没有为请求中的参数参数补充信息。我得到的结果很好。有谁知道参数参数的用途以及我应该何时补充信息?

    let editedFlowerName = flowerName.replacingOccurrences(of: " ", with: "%20", options: .literal, range: nil)
    let url = "https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&exintro&explaintext&redirects=1&indexpageids"
    guard let wikiURL = URL(string: "\(url)&titles=\(editedFlowerName)") else {fatalError("Error creating url")}
    //print(wikiURL)
    AF.request(wikiURL, method: .get).validate()
      .responseJSON { (response) in
          let json = JSON(value)
          print(json)
        }

【问题讨论】:

    标签: swift networking alamofire


    【解决方案1】:

    参数不是强制性的,除非您想随请求一起发送一些数据。使用默认编码时,请求GETDELETEHEAD 将参数编码为查询字符串并将其添加到 URL。但是,对于所有其他请求 - POSTPATCHPUTOPTIONSCONNECT - 参数被编码为查询字符串并作为请求正文发送。

    您可以通过更改编码类型来更改此行为。

    例如,通过将编码设置为queryString,您可以将编码后的查询字符串结果设置或附加到现有的查询字符串。

    URLEncoding(destination: .queryString)
    

    同样,您可以将编码查询字符串结果设置为所有使用httpBody 的请求的 URL 请求的 HTTP 正文。

    URLEncoding(destination: .httpBody)
    

    【讨论】:

      猜你喜欢
      • 2017-04-15
      • 1970-01-01
      • 2017-12-15
      • 1970-01-01
      • 2017-02-10
      • 2018-03-09
      • 2012-05-03
      • 1970-01-01
      • 2019-04-16
      相关资源
      最近更新 更多