[swift-corelibs-dev] Inconsistent URLSession POST behaviour

Michael Ferenduros mike.ferenduros at gmail.com
Sat Sep 10 15:45:20 CDT 2016

I’m seeing differences between Linux and Mac when trying to do a POST with a URLSessionDataTask - it seems like URLRequest.httpBody is ignored on Linux, so POSTs arrive with an empty body.

Was I relying on undefined behaviour, or is this just not yet implemented?

The following captures the difference in behaviour:

import Foundation
import Dispatch

var req = URLRequest(url: URL(string: "http://httpbin.org/post")!)
req.httpMethod = "POST"
req.httpBody = "Hello! You should see me in the 'form' parameter of the result.".data(using: .utf8)

let sesh = URLSession(configuration: URLSessionConfiguration.default)

let dataTask = sesh.dataTask(with: req) { data, _, _ in
	print(String(data: data!, encoding: .utf8)!)

More information about the swift-corelibs-dev mailing list