[英]Swift - multipart/form-data POST request(upload) with and without image
I am trying below code for upload profile data without image and getting 400 bad request. 我正在尝试下载代码,无需图像上传个人资料数据,并获得400个错误请求。 And also while I am uploading profiledata with file image getting same error.
而且当我上传profiledata与文件图像得到相同的错误。
some format is mismatch please help!!! 某些格式不匹配请帮忙!!!
var body = NSMutableData()
body.append("Content-Disposition: form-data; name=\"profileData\"".data(using: String.Encoding.utf8)!)
body.append("\(inputStr)\r\n".data(using: String.Encoding.utf8, allowLossyConversion: true)!)
body.append("Content-Disposition: form-data; name=\"file\"".data(using: String.Encoding.utf8)!)
body.append("\("")\r\n".data(using: String.Encoding.utf8, allowLossyConversion: true)!)
request.httpBody = body as Data
request.httpMethod = "POST"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let body = NSMutableData()
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
let strPhotoUrl = photourl
let mimetype = "image/jpeg"
let readPath = Utility.getPhotofolder().stringByAppendingPathComponent(pathComponent:strPhotoUrl) // pass the path of the image
let image = UIImage(named: strPhotoUrl)
if (image == nil)
{
print("image is nil")
return
}
let image_data = UIImagePNGRepresentation(image!)
if(image_data == nil)
{
return
}
body.append("Content-Disposition:form-data; name=\"attachedImage\"; filename=\"\(photourl)\"\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append(image_data!)
body.append("\r\n".data(using: String.Encoding.utf8)!)
body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)
request.httpBody = body as Data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.