2

Trying to upload image to server but didn't succeed, here is my code, nothing happened, couldn't figure why. What could be wrong?

import Foundation
    
    extension URLRequest {
      
      mutating func setMultipartFormDataBody(params: [String: (Data?, filename: String?)]) {
        
        let boundary = UUID().uuidString
        
        self.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
        
        var body = Data()
        for (key, (data, filename)) in params {
          body.append("--\(boundary)\r\n")
          
          
          if let filename = filename {
            body.append("Content-Disposition: form-data; name=\"\(key)\"; filename=\"\(filename)\"\r\n")
          }
          else {
            body.append("Content-Disposition: form-data; name=\"\(key)\"\r\n")
          }
          
          body.append("\r\n")
          body.append(data ?? Data())
          body.append("\r\n")
        }
        
        body.append("--\(boundary)--")
        self.httpBody = body
      }
      
    }
    
    extension Data {
      mutating func append(_ s: String) {
        self.append(s.data(using: .utf8)!)
      }
    }

thats even not posting string to server added request task but the body ignored, must set the boundry and low level? URLSESSEION is not like ALAMOFIRE?

  let boundary = "Boundary-\(UUID().uuidString)"
  
  var request = URLRequest(url: URL(string: "url")!)
  request.httpMethod = "POST"
  request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
  request.httpBody = httpBody as Data
  URLSession.shared.dataTask(with: request) { data, response, error in
    // handle the response here
  }.resume()
chkml
  • 61
  • 5
  • 1
    And where is called setMultipartFormDataBody() ? That's only the URL modifications, not URLSession code. – Larme Aug 18 '22 at 19:19
  • show us the code you use to call the server, not just the small bit you show. See also a how to present minimal code, https://stackoverflow.com/help/minimal-reproducible-example – workingdog support Ukraine Aug 18 '22 at 22:22
  • You don't call `setMultipartFormDataBody()` in your edited question. You even set a new boudary? – Larme Aug 19 '22 at 09:40
  • Does this answer your question? [Upload image with multipart form-data iOS in Swift](https://stackoverflow.com/questions/29623187/upload-image-with-multipart-form-data-ios-in-swift) – workingdog support Ukraine Aug 19 '22 at 10:55
  • Thanks for answer, i tried that link examples, its work, but does it the correct way for upload? must set boundry and other stuff? is it better use alamofire? – chkml Aug 19 '22 at 11:44
  • the first and most popular answer suggests `...No Need to use any library for upload images using multipart request...`. See also this gist: https://gist.github.com/nnsnodnb/efd4635a6be2be41fdb67135d2dd9257 – workingdog support Ukraine Aug 19 '22 at 12:11
  • "must set boundry and other stuff? is it better use alamofire? " If you use multiform part data, yes, you need the boundaries. Alamofire just does it for you, in its internal code, that's all. – Larme Aug 19 '22 at 13:32
  • Ok thank you all for answers – chkml Aug 19 '22 at 14:14

0 Answers0