Crashalot Crashalot - 1 year ago 73
iOS Question

iOS: HTTP POST request times out when setting httpBody property

The code below is based on this excellent SO answer for making POST requests on iOS. This code attempts to make a POST request to:

The request works if there is no value for the
property of the
object, or if the value is only the empty string.

However, once a non-empty value is assigned to
, the request times out.

Anyone know why?

// Succeeds
let userOptions = ["url": ""]
userOptions["bodyString" : ""]
doPost(userOptions: userOptions)

// Fails
let userOptions = ["url": ""]
userOptions["bodyString" : "hi"]
doPost(userOptions: userOptions)

fileprivate func doPost(userOptions: [String:Any]) {
// Get URL string
let urlString = userOptions["url"] as? String ?? ""

// If invalid URL, return early
guard let url = URL(string: urlString) else {
printError("Error in doPost: malformed URL")

// If here, URL valid so execute request
// Create request
var request = URLRequest(url: url)

// Add headers
if let tHeaders = userOptions["headers"] as? [String:String] {
for (key, value) in tHeaders {
request.setValue(value, forHTTPHeaderField: key)

// Add body data
let bodyString = userOptions["bodyString"] as? String ?? ""
request.httpBody = .utf8)

// Create data task
let task = URLSession.shared.dataTask(with: request) { data, response, error in
// If networking error, return early
guard let data = data, let httpResponse = response as? HTTPURLResponse, error == nil else {
printError("Error with network while executing POST request: \(error)")

// Why, oh why, does it not reach here???

// Start data task
print("Executing HTTP request: \(request)")

Rob Rob
Answer Source

If you want to do a POST request, not only do you want to set the httpBody, but you also want to set the httpMethod of the request to POST.

request.httpMethod = "POST"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download