Skip to content

SR-2617: URLSession.dataTask() ignores httpBody #633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2016

Conversation

seabaylea
Copy link
Contributor

SR-2617 reports that if you create a URLRequest with a httpBody set, and then try to create a session using URLSession.dataTask(), the httpBody is ignored.

This is because the call to URLSession.dataTask() creates a task with the following constructor:

URLSessionDataTask(session: self, request: r, taskIdentifier: i)

which in turn calls:

init(session: session, request: request, taskIdentifier: taskIdentifier, body: .none)

ie, we create a task with no body.

I've therefore made a change to pass through URLRequest.httpBody if one exists.

The reported bug has a second issue: the request is a POST, and we're not setting "Content-Type" : "application/x-www-form-urlencoded". I'll look at fixing that separately.

@parkera
Copy link
Contributor

parkera commented Sep 13, 2016

@swift-ci please test

@parkera parkera merged commit 64f5359 into swiftlang:master Sep 14, 2016
pushkarnk pushed a commit to pushkarnk/swift-corelibs-foundation that referenced this pull request Sep 23, 2016
pushkarnk pushed a commit to pushkarnk/swift-corelibs-foundation that referenced this pull request Sep 23, 2016
parkera pushed a commit that referenced this pull request Sep 26, 2016
* Test case to test TimeZone.localizedName

* Allow httpAdditionalHeaders to contain Strings or NSStrings (#625)

* Switch to Error from NSError for API conformance

* Fix for a URLSession crash (SR-2630)

* If present, pass URLRequest.httpBody to the dataTask (#633)

* Set Content-Type to application/x-www-form-urlencoded for POST requests

* Update NSURLSession.swift

* Fix for SR-2631 and implementation of URLSession.finishTasksAndInvalidate

* Allow numeric types in NSNumberFormatter.string(for:)[SR-2477] (#643)

* Fix for [SR-2486] Crash in NSDictionary.description
@zmeyc
Copy link

zmeyc commented Nov 29, 2016

In some situations httpBody is still not sent. Below is the code which reproduces the issue. If "Content-Type" line is uncommented, second dataTask won't send httpBody:

import Foundation
import Dispatch

var req = URLRequest(url: URL(string: "http://httpbin.org/post")!)
req.httpMethod = "POST"
req.httpBody = "a=b&c=d".data(using: .utf8)
//req.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")

let sesh = URLSession(configuration: URLSessionConfiguration.default)

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

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

dispatchMain()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants