I am trying to upload an image with json data. I just followed the post and implemented the mechanism. But I get the error
{ status code: 400, headers {
"Cache-Control" = "no-cache, no-store, max-age=0, must-revalidate";
Connection = close;
"Content-Language" = en;
"Content-Length" = 1033;
"Content-Type" = "text/html;charset=utf-8";
Date = "Wed, 27 Jan 2016 10:44:34 GMT";
Expires = 0;
Pragma = "no-cache";
Server = "Apache-Coyote/1.1";
"X-Content-Type-Options" = nosniff;
"X-XSS-Protection" = "1; mode=block";
} }`
Below is the complete HTTP Request
Content-Type: multipart/form-data;boundary=Boundary_123456789
Authorization: Basic bihdwbcIUkbcdwjnoNOn
User-Agent: Jersey/2.21.1 (HttpUrlConnection 1.8.0_45)
MIME-Version: 1.0
Host: localhost:8080
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 3526
--Boundary_123456789
Content-Type: application/json
Content-Disposition: form-data; name="userDTO"
{"id":"id","name":"name","age":23}
--Boundary_123456789
Content-Type: image/png
Content-Disposition: form-data; filename="sample-image2.png"; modification-date="Fri, 22 Jan 2016 04:56:48 GMT"; size=3308; name="file"
‰PNG
<Binary data>
--Boundary_123456789—
Below is my implementation
func addUser(completion: (message: String?, error: String?) -> Void) -> NSURLSessionDataTask {
// create the request
let request = createRequest()
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
print(response) // 400 Error is printed here
}
task.resume()
return task
}
func createRequest () -> NSURLRequest {
let param = [
"id": "id",
"name": "name",
"age": 23] // build your dictionary however appropriate
let boundary = generateBoundaryString()
let url = NSURL(string: SERVERURL)!
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.addValue("Basic \(base64LoginString())", forHTTPHeaderField: "Authorization")
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let path1 = NSBundle.mainBundle().pathForResource("userImage", ofType: "png") as String!
request.HTTPBody = createBodyWithParameters(param, paths: [path1], boundary: boundary)
return request
}
func createBodyWithParameters(json: [String:AnyObject], paths: [String]?, boundary: String) -> NSData {
let body = NSMutableData()
let key = "userDTO"
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Type: application/json")
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
var requestBody = NSData()
do {
requestBody = try NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions(rawValue:0))
} catch (let e) {
print(e)
}
body.appendData(requestBody)
//body.appendString("\(json)\r\n")
if paths != nil {
for path in paths! {
let url = NSURL(fileURLWithPath: path)
let data = NSData(contentsOfURL: url)!
let mimetype = mimeTypeForPath(path)
let fileName = "sample-image23.png"
let date = "Fri, 22 Jan 2016 04:56:48 GMT"
let name = "file"
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Type: \(mimetype)\r\n\r\n")
body.appendString("Content-Disposition: form-data; filename=\"\(fileName)\"; modification-date=\"\(date)\"; size=3308; name=\"\(name)\"\r\n")
body.appendData(data)
body.appendString("\r\n")
}
}
body.appendString("--\(boundary)--\r\n")
return body
}
func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().UUIDString)"
}
func mimeTypeForPath(path: String) -> String {
let url = NSURL(fileURLWithPath: path)
let pathExtension = url.pathExtension
if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension! as NSString, nil)?.takeRetainedValue() {
if let mimetype = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() {
return mimetype as String
}
}
return "application/octet-stream";
}
Also the extension
extension NSMutableData {
func appendString(string: String) {
let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
appendData(data!)
}
}
I guess I am making some mistake in the json format. Someone can help? Thanks!