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 :)
NSPOSIXErrorDomain error might be related to third-party applications. Uninstall third-party firewalls, antivirus and other internet security programs. Open Finder and go to Applications, chose the particular program, right-click and select Move to Trash. Then empty the Trash.
When you try to log in to the desktop app, you might receive an error message that says: "SOCKS version 5 protocol error". This error message indicates that the problem is caused by the proxy settings on your Mac. To solve this, try the following steps: On your Mac, click System Preferences. Click Network.
The message implies the error has to do with available disk/memory space on the device. However, after checking both there is no link to be found since there is plenty of space available. Also, the error occurs on multiple devices, all running iOS 14.4 or higher.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With