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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…