简体   繁体   English

Swift - 包含和不包含图像的multipart / form-data POST请求(上传)

[英]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!!! 某些格式不匹配请帮忙!!!

Postman Request Image 邮差申请图片

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.

相关问题 在 Swift 中上传带有多部分表单数据 iOS 的图像 - Upload image with multipart form-data iOS in Swift 仅在 Swift 4.2 中上传带有多部分表单数据的图像 - Upload image with multipart form-data only in Swift 4.2 如何使用Swift执行多部分/表单数据发布请求? - How to do multipart/form-data post request with Swift? swift url Session 带有参数和图像的发布请求(多部分表单数据请求) - swift url Session post request with parameters and image (Multipart Form-Data Requests ) 如何在 Swift 中使用 Codable 和 URLSession.shared.uploadTask (multipart/form-data) 上传图像文件? - How to upload image file using Codable and URLSession.shared.uploadTask (multipart/form-data) in Swift? 上传文件时,无需手动使用“ multipart / form-data”构建HTTP POST; 边界” - Upload file without manually constructing HTTP POST with “multipart/form-data; boundary” 在Swift中上传包含多部分/表单数据的图像和参数 - Uploading an image and parameters with multipart/form-data in Swift 获取System.InvalidOperationException:请求格式无效:通过AFNetworking上传图像(数据)时出现multipart / form-data错误 - Getting System.InvalidOperationException: Request format is invalid: multipart/form-data error while upload image(data) via AFNetworking 在Swift中添加边界后,多部分表单数据POST无法上传图片 - Multipart-form data POST to upload image not working after boundary is added in Swift 快速上传多部分/表单数据,并将字符串数组附加到主体(问题:数组作为字符串发送到服务器) - Swift multipart/form-data upload with append String Array to body (Issue: Array goes as String to Server)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM