HTTP -> HTTPS redirects need to be handled correctly

Had the issue that a URLSession request I was POSTing wasn’t having its data arrive on the server. Figured out the server was redirecting the HTTP request to an HTTPS URL, however turning it into a GET request, dropping the POST data. The solution – wrap the request and implement

public func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
        originalRequest.url = request.url
        completionHandler(originalRequest)
    }

(from the URLSessionTaskDelegate). This fixed the issue.

Found solution on http://stackoverflow.com/questions/10783499/nsurlrequest-how-to-handle-a-redirected-post

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s