Error Domain=NSPOSIXErrorDomain Code=100 “Protocol error”

匿名 (未验证) 提交于 2019-12-03 01:26:01

问题:

I am trying to call a service using Alamofire using a .get method. The request has nothing special no parameters just an Authorization header.

I am going a bit mad here because to request works fine when I run it on postman with the same URL and Authorization token, but when I run my app code with Alamofire it returns this error:

Error Domain=NSPOSIXErrorDomain Code=100 "Protocol error" UserInfo={NSErrorPeerAddressKey={length = 16, capacity = 16, bytes = 0x100201bbd83ad0b10000000000000000}, _kCFStreamErrorCodeKey=100, _kCFStreamErrorDomainKey=1}

I am using:

Alamofire.request("https://myserverURL", method: .get, parameters: [:], encoding: JSONEncoding.default, headers: ["Authorization":"myToken"])         .responseJSON {response in       guard response.result.error == nil else {         //HERE IS WHERE IS GOING IN WITH THE ERROR     } } 

Any thoughts will be much appreciated or point me in the right direction :)

回答1:

I got exact same error as yours in Cocoa with foundation class URLSession. After hours debugging the issue lies in the HTTP request body.

You should really try to dump the HTTP request/response body to see if there are some malformed fields. For example, Content-Length and Content-Type are right or missing? In my experience if these required(fundamental) headers are malformed, it may not work depending on your OS or other intermediate network accept(e.g. proxy, gateway, server, etc.)

My fault is misplacing function params in the method URLRequest.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type"), which ends up with a wrong Content-Type HTTP field.

However, it works in macOS 10.12 but not 12.11 so you should makes sure your HTTP Request body is not malformed.

Hope it helps.


Form your sample code, I guess the encoding: JSONEncoding.default is wrong. Since an HTTP GET method has NO body, a strict/not-robust network component would reject/not understand it.

What your goal is set the Accept: application/json in the request header, however it's not required if you're sure the response body type.



回答2:

try to replace this parameters: [:] with parameters: nil in your request. Strangely it fixed the problem.



回答3:

In my case there were several F5 balancers in the test network. One of the F5 servers were validating the request incorrectly in such a way that json's objects containing @ or / were considered malformed. The issue was hard to detect because many times it was working for me, but few times it was not - exactly according to the specific misconfigured F5 server that my client app was communicating with.



回答4:

I got this error with Apache sending an upgrade header. Here is a link to Apache's bugzilla discussion on the issue.

Fixed it by adding Header unset Upgrade to host config in Apache.



回答5:

Adding ["Content-Type": "application/json"] to the headers helped.



回答6:

This was a tricky one at least for me for an entire day ?. I figure out at the end how to solve it, it was an HTTP protocol version issue with the server.

We were using IIS to connect to a development API and by default IIS if I remember correctly above 10 use HTTP 2.0 if both the client and the server support it. Also, our server was using an SSL certificate for the development API so I think this was the main issue combined with HTTP 2.0. Disabling the use of HTTP 2.0 for the IIS in the development API fixes the issue.

So my advice is to try to monitor the incoming request to see which kind of protocol is using the request in the server, you can do it with Charles or any another HTTP monitor tool.

I hope this helps someone else with the same problem.



回答7:

I had the same issue fixed it by replacing Authorization feild value to nil instead of ""

[request setValue: @"" forHTTPHeaderField:@"Authorization"]; 

changed it to this

[request setValue: nil forHTTPHeaderField:@"Authorization"]; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!