【问题标题】:Request REST POST API work in Postman but in Alamofire don't work请求 REST POST API 在 Postman 中工作,但在 Alamofire 中不起作用
【发布时间】:2023-05-22 01:21:01
【问题描述】:

我有一个返回非常简单 JSON 的 API。

首先包含 api 的服务器在我的本地机器(localhost)中,在这种情况下,一切正常。

当我尝试从 iPhone 应用程序(使用 Alamofire)连接到托管 api 时,我将 api 传递给了虚拟主机,但无法正常工作

Alamofire.request(.POST, "https://domain.com/api/search", parameters: ["ubicacion": ubicacionId, "fecha":"\(dateFormatter.stringFromDate(fecha))"], encoding: .URL)
            .responseJSON { response in
                switch response.result {
                case .Success:
                    print(response.request)  // original URL request
                    print(response.response) // URL response
                    print(response.result)   // result of response serialization
                    self.JSON = response.result.value!
                    //llamar el delegate
                    self.delegate.devolverjson(self.JSON)
                case .Failure(let error):
                    print(response.request)  // original URL request
                    print(response.response) // URL response
                    print(response.result)   // result of response serialization
                    self.JSON = ["error":error]
                    //llamar el delegate
                    self.delegate.devolvererror(self.JSON)
                }
        }

在这种情况下,response.response 是:

{ URL: https://domain.com/api/search } { status code: 405, headers {
    "Cache-Control" = "no-cache, private";
    Connection = "Keep-Alive";
    "Content-Type" = "text/html; charset=UTF-8";
    Date = "Thu, 07 Jul 2016 18:24:26 GMT";
    "Keep-Alive" = "timeout=3, max=30";
    Server = "Apache Phusion_Passenger/4.0.10 mod_bwlimited/1.4 mod_fcgid/2.3.9";
    "Transfer-Encoding" = Identity;
    "X-Powered-By" = "PHP/5.6.22";
    allow = POST;
} })

response.result.Failure

错误是

error: {
    error = "Error Domain=NSCocoaErrorDomain Code=3840 \"Invalid value around character 0.\" UserInfo={NSDebugDescription=Invalid value around character 0.}";
}

当我从 Postman 连接到 api(相同的 URL:https://domain.com/api/search)时,api 会返回正确的信息 (JSON),并带有此标头。

Cache-Control →no-cache
Connection →Keep-Alive
Content-Type →application/json
Date →Thu, 07 Jul 2016 18:23:38 GMT
Keep-Alive →timeout=3, max=30
Server →Apache Phusion_Passenger/4.0.10 mod_bwlimited/1.4 mod_fcgid/2.3.9
Transfer-Encoding →chunked
X-Powered-By →PHP/5.6.22

如果 API 与 Postman 一起使用,为什么不与 Alamofire 一起使用?

如果英文不正确,请见谅

【问题讨论】:

  • 我认为您的邮递员参数密钥与请求密钥不同
  • 密钥相同,但如果密钥不同,则内容类型应为 JSON @AkshanshThakur
  • 有同样的问题

标签: swift alamofire postman


【解决方案1】:

我今天遇到了类似的问题。我的解决方法是更改​​请求的编码。

我在URLEncoding()JSONEncoding.default 之间切换

您可以尝试适合您要求的各种其他编码。

【讨论】:

  • 你拯救了我的一天 :)
【解决方案2】:

尝试在您的网址中添加斜杠:

https://domain.com/api/search/

【讨论】:

  • 为什么以及如何? :/为我工作,但似乎很烦人的解决方案
【解决方案3】:

response.request 是否与您在 Postman 中尝试的 url 匹配? 您是否尝试过 Alamofire.request(.GET, url) 而不是 .POST? 我在一个项目中的参数有一些问题,所以我之前格式化了我的 url,并使用了没有参数但使用格式化的 url 的 Alamofire。

【讨论】:

  • 是的,是同一个 URL。当我尝试使用 .GET 而不是 .POST 时,状态码是 403 而不是 405,使用我 POST 时得到的
【解决方案4】:

我在 Swift 4.2 Xcode 10 中遇到了同样的错误。

我在 Postman 中使用 URL 为:18.222.156.118/Service/v1/SignIn 并传递参数

UserPhone:7021844116 RequestCd:R28

它在 Postman 中正常工作。

当我在 Alamofire 中使用相同的 URL 时

    let parameters: Parameters = ["UserPhone": self.textfieldPhoneNumber.text!,
                                  "RequestCd": "R28"]

    // All three of these calls are equivalent
    Alamofire.request("18.222.156.118/Service/v1/SignIn",method: .post, parameters: parameters, encoding: URLEncoding.default).responseJSON { response in
        print("Result: \(String(describing: response.request))")
        print("Result: \(String(describing: response.response))")
        print("Result: \(response.result)")
        MBProgressHUD.hide(for: self.view, animated: true)

        if let json = response.result.value as? [String:Any] {
            print("JSON VALUE --- \(json)")
            if json["ErrorCode"] as?  Int ==  0 {
              print("got success")

            }else{
                 Print("error")
            }
        }
    }

我收到错误Domain=NSURLErrorDomain Code=-1002 "unsupported URL" 我最初添加了http://

正确的网址是http://18.222.156.118/Service/v1/SignIn 它终于奏效了。 由于 google Postman 内部添加了 hhttp:\.

【讨论】:

    最近更新 更多