0

Im trying to upload a file to the backend. edit

I'm at a stand still with this code. I feel like I'm doing this wrong. Its returning (Status Code: 400, Headers ) Which makes me believe that the error is in the headers but I may be totally wrong because I see people using .upload() instead of .request. I've made 2 functions based on these two methods, neither one works. .request is error 400, upload() is error 500. I've tried the access token in L5 Swagger and it works. The only parameter is a file which I assume is data. Here are the methods

@discardableResult
  public func postUploadSeekerAvatar(_ image: UIImage, result: @escaping (Error?) -> Void) -> URLSessionTask? {
    let imageData: Data = UIImagePNGRepresentation(image)!
    let params: [String: Any] = ["data": imageData]
    var headers = authHeader
    headers?["Content-Type"] = "application/json"
    return Alamofire.request(endpointURL("users/seeker/avatar"), method: .post, parameters: params, headers: headers)
      .responseData(completionHandler: { (response: DataResponse<Data>) in
        guard response.result.error == nil else {
          return result(response.result.error)
        }
        return result(response.result.error)
      }).task
  }

  @discardableResult
  public func postSeekerAvatar(_ image: UIImage, result: @escaping (Error?) -> Void) -> URLSessionTask? {
    let data = UIImagePNGRepresentation(image)!
    var headers = authHeader
    headers?["Content-Type"] = "application/json"
    return Alamofire.upload(data, to: endpointURL("users/seeker/avatar"), method: .post, headers: headers)
      .responseData(completionHandler: { (response: DataResponse<Data>) in
        guard response.result.error == nil else {
          return result(response.result.error)
        }
        return result(response.result.error)
      }).task
  }

Can you sirs and madams please help a newbie? Thank you!

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
CRey
  • 1,150
  • 2
  • 9
  • 21
  • Your question is quite unclear. Are you trying to upload an image from your iOS device to a server using Alamofire? – jms Feb 27 '19 at 04:40
  • @jms yes I am. parameters needed from server is only the image data. – CRey Feb 27 '19 at 04:41
  • Are you sure that your server endpoint works correctly for uploading images? You can verify this by making a simple file upload html page and directing it to your server. – jms Feb 27 '19 at 04:44
  • Im going to assume you're familiar with swagger. Anyways, the android side is able to upload and download the images and I've tried the access token in L5 swagger and it works. I feel the error is on my end. – CRey Feb 27 '19 at 04:47
  • You may need `Content-Type: multipart/form-data` as a request header – jms Feb 27 '19 at 04:47
  • even though it says, response content type is application/json? well, doesn't hurt to try. hehe – CRey Feb 27 '19 at 04:49
  • 1
    https://stackoverflow.com/questions/26497725/how-to-upload-image-with-parameters-using-alamofire-in-swift/48841037#48841037 – Arjun Patel Feb 27 '19 at 04:50
  • 1
    Also this https://medium.com/swift2go/alamofire-4-multipart-file-upload-with-swift-3-174df1ef84c1. Hope it helps. – jms Feb 27 '19 at 04:51
  • thank you so much both of you. I try that method. – CRey Feb 27 '19 at 04:52
  • 1
    Please understand that there's a difference between requests content-type and response content-type. Your file upload is a request at your end and should have headers set as multipart/form-data to work. The response you receive from this file upload to the server may be content-type application/json. This does not mean you should use the response content-type documented in swagger for your request as well. – jms Feb 27 '19 at 04:55

0 Answers0