Skip to content

Commit

Permalink
Send POST parameters as data instead of JSON, fixes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdonnelly committed Jun 22, 2014
1 parent d689f3a commit 608e6fe
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
19 changes: 16 additions & 3 deletions SwifterCommon/Dictionary+Swifter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,26 @@ extension Dictionary {
return filteredDictionary
}

func queryStringWithEncoding() -> String {
var parts = String[]()

for (key, value) in self {
let keyString: String = "\(key)"
let valueString: String = "\(value)"
let query: String = "\(keyString)=\(valueString)"
parts.append(query)
}

return parts.bridgeToObjectiveC().componentsJoinedByString("&") as String
}

func urlEncodedQueryStringWithEncoding(encoding: NSStringEncoding) -> String {
var parts = String[]()

for (key, value) in self {
let keyString = "\(key)".urlEncodedStringWithEncoding(encoding)
let valueString = "\(value)".urlEncodedStringWithEncoding(encoding)
let query = "\(keyString)=\(valueString)" as String
let keyString: String = "\(key)".urlEncodedStringWithEncoding(encoding)
let valueString: String = "\(value)".urlEncodedStringWithEncoding(encoding)
let query: String = "\(keyString)=\(valueString)"
parts.append(query)
}

Expand Down
20 changes: 9 additions & 11 deletions SwifterCommon/SwifterHTTPRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class SwifterHTTPRequest: NSObject, NSURLConnectionDataDelegate {
self.HTTPMethod = request.HTTPMethod
self.headers = [:]
self.parameters = [:]
self.encodeParameters = false
self.encodeParameters = true
self.uploadData = []
self.dataEncoding = NSUTF8StringEncoding
self.timeoutInterval = 60
Expand Down Expand Up @@ -149,21 +149,19 @@ class SwifterHTTPRequest: NSObject, NSURLConnectionDataDelegate {
self.request!.setValue("application/x-www-form-urlencoded; charset=\(charset)", forHTTPHeaderField: "Content-Type")
}
else {
var queryString = String()
if self.encodeParameters {
let queryString = nonOAuthParameters.urlEncodedQueryStringWithEncoding(self.dataEncoding)
self.request!.URL = self.URL.URLByAppendingQueryString(queryString)
queryString = nonOAuthParameters.urlEncodedQueryStringWithEncoding(self.dataEncoding)
self.request!.setValue("application/x-www-form-urlencoded; charset=\(charset)", forHTTPHeaderField: "Content-Type")
}
else {
var error: NSError?
if let jsonData: NSData = NSJSONSerialization.dataWithJSONObject(nonOAuthParameters, options: nil, error: &error) {
self.request!.setValue("application/json; charset=\(charset)", forHTTPHeaderField: "Content-Type")
self.request!.HTTPBody = jsonData
}
else {
println(error!.localizedDescription)
}
queryString = nonOAuthParameters.queryStringWithEncoding()
}

let data = queryString.dataUsingEncoding(self.dataEncoding)

self.request!.setValue(String(data.length), forHTTPHeaderField: "Content-Length")
self.request!.HTTPBody = data
}
}
}
Expand Down
1 change: 1 addition & 0 deletions SwifterCommon/SwifterOAuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class SwifterOAuthClient: SwifterClientProtocol {
request.successHandler = success
request.failureHandler = failure
request.dataEncoding = self.dataEncoding
request.encodeParameters = postData == nil

if postData {
let fileName = postDataFileName ? postDataFileName! as String : "media.jpg"
Expand Down

0 comments on commit 608e6fe

Please sign in to comment.