Multipart Image Alamofire 4 swift 3 -
i've big problem when try update image db.
i must upload image, , string value.
func imagepickercontroller(_ picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [string : any]) { if let selectedimage = info[uiimagepickercontrolleroriginalimage] as? uiimage { imagecropped = selectedimage self.sendimagewithmultipart() imagepicker.dismiss(animated: true, completion: { _ in }) } } func sendimagewithmultipart() { alamofire.upload(multipartformdata: { multipartformdata in multipartformdata.append("\(hgygjihf746fg743g)".data(using: string.encoding.utf8, allowlossyconversion: false)!, withname :"auth_token") multipartformdata.append("\(0)".data(using: string.encoding.utf8, allowlossyconversion: false)!, withname :"os") multipartformdata.append("\(10.0.3)".data(using: string.encoding.utf8, allowlossyconversion: false)!, withname :"os_version") multipartformdata.append("\(3.0.3)".data(using: string.encoding.utf8, allowlossyconversion: false)!, withname :"app_version") if let photo = self.imagecropped, let jpegimage = uiimagejpegrepresentation(photo, 50.0) { print(jpegimage) multipartformdata.append(jpegimage, withname: "profile_photo", mimetype: "image/*") } }, to: "\(constants.general_address_2)\(constants.api_modifyuser_profile_photo)", method:.post, headers:["content-type":"multipart/form-data; charset=utf-8; boundary=__x_paw_boundary__"], encodingcompletion: { encodingresult in switch encodingresult { case .success(request: let upload, streamingfromdisk: _, streamfileurl: _): upload.validate().responsejson { response in if response.result.isfailure { debugprint(response) } else { debugprint(response) } } case .failure(let encodingerror): nslog((encodingerror nserror).localizeddescription) } }) }
but result this:
[request]: "\(constants.general_address_2)\(constants.api_modifyuser_profile_photo)" [response]: <nshttpurlresponse: 0x17023c0e0> { url: "\(constants.general_address_2)\(constants.api_modifyuser_profile_photo)" } { status code: 500, headers { "cache-control" = "no-cache, private"; connection = close; "content-length" = 5545; "content-type" = "text/html; charset=utf-8"; date = "thu, 10 nov 2016 10:49:00 gmt"; server = "apache/2.4.18 (amazon) openssl/1.0.1k-fips php/5.6.21"; "x-powered-by" = "php/5.6.21"; } } [data]: 5545 bytes [result]: failure: responsevalidationfailed(alamofire.aferror.responsevalidationfailurereason.unacceptablestatuscode(500)) [timeline]: timeline: { "request start time": 500467740.667, "initial response time": 500467740.793, "request completed time": 500467743.733, "serialization completed time": 500467743.733, "latency": 0.126 secs, "request duration": 3.066 secs, "serialization duration": 0.000 secs, "total duration": 3.066 secs
the problem how convert image, or content type or what?
thank you
this may potentially resolve issue:
let endpoint = "https://api.blah/images/" + id + "/documents" let imagedata = uiimagepngrepresentation(image) alamofire.upload( multipartformdata: { multipartformdata in multipartformdata.append(type.data(using: string.encoding.utf8, allowlossyconversion: false)!, withname :"type") multipartformdata.append(imagedata!, withname: "file.png", mimetype: "image/png") }, to: endpoint, method:.post, headers:headers, encodingcompletion: { encodingresult in switch encodingresult { case .success(request: let upload, streamingfromdisk: _, streamfileurl: _): upload.validate().responsejson { (response) in if response.result.isfailure { debugprint(response) } else { debugprint(response) } } case .failure(let encodingerror): nslog((encodingerror nserror).localizeddescription) } })
seems reach server, in case, 400 (probably failing tv4 validation).
Comments
Post a Comment