简体   繁体   English

带有JSON响应的Alamofire错误

[英]Alamofire error with JSON response

I have been struggling with Alamofire issue for days and I'm not sure if it's from the backend or the way I structure my code. 我已经为Alamofire问题苦苦挣扎了好几天了,我不确定这是从后端还是我的代码构造方式。

Here is the issue: 这是问题所在:

when I text my API with Postman I get the correct response if I hit the params button and add the parameters Notice the lower body is empty but the upper one is filled with parameters 当我用Postman发短信给我的API时,如果我按下params按钮并添加参数,我将得到正确的响应。 请注意,下半部分为空,而上半部分为参数

在此处输入图片说明

However, when I use the lower parameters with the same info I get an error with no JSON. 但是,当我将较低的参数与相同的信息一起使用时,会收到不带JSON的错误。 Also same error appears in Xcode when I try to call the API with the same link 当我尝试使用相同的链接调用API时,Xcode中也会出现相同的错误

this is Xcode Alamofire error 这是Xcode Alamofire错误

FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})) 失败:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误域= NSCocoaErrorDomain代码= 3840“字符0周围的无效值。” UserInfo = {NSDebugDescription =字符0周围的无效值。)))

and here is Postman error 这是邮递员错误

在此处输入图片说明

and finally this is my code to call API: 最后这是我调用API的代码:

    let urlStr = "api/client/meal_add"
    let url = URL(string: urlStr)
    let para = ["meal_id": "31",
                "user_id": "2",
                "qty":"2"]

    let headers: HTTPHeaders = [
        "Auth": "Auth" // Edited for security
    ]

    Alamofire.request(url!, method: .post, parameters: para,encoding: URLEncoding.default, headers: headers).responseJSON { response in
        print(response)
        print(response.result.debugDescription)
        if let value : AnyObject = response.result.value as AnyObject {

it used to work perfectly 5 days ago but I have no idea why it stoped. 它曾经在5天前完美运行,但我不知道为什么它停止了。 Also In other View Controllers I do HTTP calls to the same API and the responses have no issues, only this one. 另外,在其他View Controller中,我对同一API进行HTTP调用,并且响应没有问题,只有这个问题。

First, I want to talk about the difference between HTTP get and post request. 首先,我想谈谈HTTP get和post请求之间的区别。 Get request is putting parameters in url. 获取请求将参数放入url中。 They are appended to the url in this format 它们以这种格式附加到URL

URL?key1=value1&key2=value2

The RESTful hanlder for that URL will then parse the key value pair and use them. 然后,该URL的RESTful处理程序将解析键值对并使用它们。 When you click on the param button in your postman, it actuall doing this append param to url thing. 当您单击邮递员中的param按钮时,实际上是将此附加param附加到url上。 However this get into problem when you have lots of param. 但是,当您有很多参数时,这会出现问题。 Usually, an url will have some problem if it exceeds length 8000. So a post request is actually nesting the parameters inside the request, not on the url. 通常,如果URL的长度超过8000,就会遇到一些问题。因此,发布请求实际上是在请求内部而不是URL上嵌套参数。 So normally in a post request, you will only see the URL, not the ?key1=value1&key2=value2 part. 因此,通常在发布请求中,您只会看到URL,而不是?key1=value1&key2=value2部分。

Back to your problem , I have no idea how your server side handles this post request with parameters in url but since an empty post request with param in url works, in your swift code, you can simply append the param in your url and do a post request with no parameters. 回到您的问题 ,我不知道您的服务器端如何使用url中的参数来处理该post请求,但是由于在url中具有param的空post请求可以工作,因此在您的快速代码中,您只需将param附加到url中并执行没有参数的发布请求。

let urlStr = "api/client/meal_add?meal_id=31&user_id=2&qty=2"
let url = URL(string: urlStr)

Alamofire.request(url!, method: .post, parameters: [],encoding: URLEncoding.default, headers: headers).responseJSON { response in

Moving forward, I strongly recommand you to have a look at your server side to make sure your post request is written in the correct way 展望未来,我强烈建议您查看服务器端,以确保您的帖子请求以正确的方式编写

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM