I want to post the customer signature image to amazon s3 bucket_type server with an api...I am referring with different tutorials...I didn't get enough information from any of tutorials...I am trying with following code but I am getting nil result...
funcUpload(service:String,imagedata:Data,completion:@escaping (_ result:NSDictionary?,_ error:AnyObject?) -> ()){
let boundaryConstant = "----------V2y2HFg03eptjbaKO0j1"
let urlpath:String = "http:myurl" + service
let url:NSURL = NSURL(string:urlpath)!
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "P0ST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringLocalCacheData
request.httpShouldHandleCookies = false
let contentType = "multipart/form-data; boundary=\(boundaryConstant)"
request.setValue(contentType, forHTTPHeaderField: "Content-Type"
let body = NSMutableData()
if imagedata != nil{
body.append("--\(boundaryConstant)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition: form-data; name=\"\("universalapparealproduct")\" ; filename=\"image.jpg\"\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Type: \("universalapparealproduct")\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append(imagedata)
body.append("\r\n".data(using: String.Encoding.utf8)!)
}
body.append("--\(boundaryConstant)--\r\n".data(using: String.Encoding.utf8)!)
request.httpBody = body as Data
let config = URLSessionConfiguration.ephemeral
self.session = URLSession(configuration: config)
let task = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
if let receivedData = data
{
do
{let resultDic = try JSONSerialization.jsonObject(with: receivedData, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
completion(resultDic!,nil)
}
catch let error
{
NSLog("\(error)")
}
}
else if let errorMessage = error
{
completion(nil,errorMessage as AnyObject?)
}
self.session.invalidateAndCancel()
}
task.resume()
}
}
Please can any one help me to resolve this issue. Thanks in advance.
let urlpath:String = "http:myurl" + service
looks like a typo. PUT
not POST
Content-Length
. It must be set to the size of the body that you send.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.