How URLRequest set content-length of HTTP header?

When use URLRequest, set httpMethod to GET, and set some no-empty Data to HTTPBody, is that correct that URLRequest just calculate content-length by HTTPBody.count? Because when i check RFC of HTTP, it's said that there is no-meaning of messageBody in Get request, so should it be zero when URLRequest found that HTTPMethod is Get?

When use URLRequest

Are you targeting an Apple platform? Or Linux?

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

Target is Apple Platform, iOS.

As of the 2019 OSes, URLRequests with a GET httpMethod and non-nil body data is considered an error, and URLSession will fail any task trying to perform one. Previously, URLSession didn't throw an error but also didn't set the Content-Length when performing the request, leading it to fail. Even if you manually set Content-Length, servers don't usually support GET requests with body data anyway.

2 Likes

Thanks for your reoly.
I have some question:

What's is 2019 OSes
Is it iOS13&macOS10.15?

URLSession will fail any task trying to perform one
so URLSession will directly return error and don't send and HTTP Request to Server?

Thanks.

Yes, in iOS 13, macOS 10.15, etc., URLSession will immediately fail the task before sending it over the network, per Apple's release notes:

Previous versions will have a network failure error of some kind.

2 Likes

Thanks a lot, i should read iOS release notes carefully.