【问题标题】:Alamofire Equivalent of Postman GET request邮递员 GET 请求的 Alamofire 等效项
【发布时间】:2020-04-19 22:56:11
【问题描述】:

我正在开发一个 iOS 应用程序,我使用“Alamofire”发出 API 请求,我测试了邮递员的 API(使用 Slim 框架创建),但我无法获得 Alamofire 的邮递员的等效请求。

邮递员要求如下:

我尝试 Alamofire 如下:

  let parm = ["Password_Father": "test", "Username_Father": "test"]
        let headers :  HTTPHeaders = ["content-type": "application/json"]
        AF.request(URL(string: "http://my.domain.com/page1/login")!,
                   method: .get, // also try it as post but get error 500 as postman when using post method
                   parameters:parm,
                   encoding: JSONEncoding.default,
                   headers: headers)
            .validate(statusCode: 200..<300)
            .responseJSON { response in
                switch response.result
                {
                case .success(_) :do {
                    print("success")
                    }
                case .failure(let error):
                    print("failure(error)",error)

                    break
                }
        }

但我收到此错误failure(error) urlRequestValidationFailed(reason: Alamofire.AFError.URLRequestValidationFailureReason.bodyDataInGETRequest)

我怎样才能得到Alamofire对邮递员的同等要求。

【问题讨论】:

  • 可能缺少某些标题?在邮递员中,标签显示(9),但代码中只有一个?
  • stackoverflow.com/questions/60960976/… :您需要联系 API 提供商并请求更改,因为没有运行 Apple 2019 操作系统的设备能够提出这样的请求。
  • @clashsoft 我没有在标头参数中放任何东西,但邮递员放了诸如缓存之类的东西。
  • 我来宾说这是https 与http 的问题。 http 需要在 Info.plist 中进行一些配置。

标签: ios swift postman alamofire slim


【解决方案1】:

google后发现this。


添加域异常

添加域例外很容易。您添加 NSExceptionDomains 目标的 NSAppTransportSecurity 字典的键 Info.plist。键的值是一个字典,每个键为 字典是域例外。看看以下 举例说明。

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <false/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>cocoacasts.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
   </dict>
</dict>

在上面的示例中,它添加了一个子域cocoacasts.com,并设置NSExceptionAllowsInsecureHTTPLoads 以允许对该子域的http 请求。 post开头就说了。

Apple 最近宣布提交到 App Store 的每个构建 需要从 1 月 1 日开始启用 App Transport Security 2017.

这意味着默认情况下所有通信都运行https。如果应用需要http,则需要为这些异常子域配置Info.plist。

【讨论】:

  • 我之前也是这样做的,和http或https无关,和请求方式有关,尤其是GET
【解决方案2】:

正如您在问题中所述,您正在发出“GET”请求。

GET 请求不能有消息正文。但是您仍然可以使用 URL 参数将数据发送到服务器。在这种情况下,您被限制为 URL 的最大大小,大约为 2000 个字符。

解决此问题的一种方法是在请求中使用URLEncoding.default 作为encoding。

您的要求如下:

let parm = ["Password_Father": "test", "Username_Father": "test"]
        let headers :  HTTPHeaders = ["content-type": "application/json"]
        AF.request(URL(string: "http://my.domain.com/page1/login")!,
                   method: .get,
                   parameters:parm,
                   encoding: URLEncoding.default,
                   headers: headers)
            .validate(statusCode: 200..<300)
            .responseJSON { response in
                switch response.result
                {
                case .success(_) :do {
                    print("success")
                    }
                case .failure(let error):
                    print("failure(error)",error)

                    break
                }
        }

这应该可行。如果您遇到问题,请随时发表评论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-29
    • 2020-07-23
    • 2018-02-10
    • 1970-01-01
    • 2019-01-07
    • 2020-03-07
    • 2019-08-25
    • 2016-12-11
    相关资源
    最近更新 更多