0

I want to make post request with Alamofire that take requests with Content-Type: multipart/form-data and accepts payload file I try to make the request as you can see from the code blow but I am getting 500 error code , I did make the same request in Postman and it work , Note after making the request I dont expected response back it will be empty

  let body: [String:String] = [
                "id":"101",
                "message":test,
                "type":"test"
            ]

            let payload = [
                "payload":body
            ]

            let headers = [ "Content-Type" : "multipart/form-data"] 
            Alamofire.request("URL", method: .post, parameters:payload, encoding: JSONEncoding.default, headers: headers).responseObject { (response: DataResponse<ObjectEntity>) in
                guard (response.response?.statusCode == 200 || response.response?.statusCode == 204) else {
                    if response.response != nil {
                        self.showAPILogs(fullURL: self.getFullURL(methodName: methodName), response: response.response, statusCode: response.response!.statusCode)

                    }
                    return
                }
                self.showAPILogs(fullURL: self.getFullURL(methodName: methodName), response: response.response, statusCode: response.response!.statusCode)


            }
Nouf
  • 733
  • 1
  • 11
  • 32
  • 2
    Possible duplicate of [Send POST parameters with MultipartFormData using Alamofire, in iOS Swift](https://stackoverflow.com/questions/31949118/send-post-parameters-with-multipartformdata-using-alamofire-in-ios-swift) – mag_zbc Apr 23 '19 at 11:15
  • You have a typo in "multipartb/form-data" – kamwysoc Apr 23 '19 at 11:17
  • but how can I send header with access token? – Nouf Apr 23 '19 at 11:51
  • Use this func of alamofire `upload( multipartFormData: @escaping (MultipartFormData) -> Void, usingThreshold encodingMemoryThreshold: UInt64 = SessionManager.multipartFormDataEncodingMemoryThreshold, with urlRequest: URLRequestConvertible, encodingCompletion: ((SessionManager.MultipartFormDataEncodingResult) -> Void)?)` – Shruti Apr 23 '19 at 12:03
  • I am getting "Required request part 'payload' is not present" – Nouf Apr 23 '19 at 12:21

1 Answers1

0

Try

let body: [String: String] = [
        "id": "101",
        "message": test,
        "type": "test"
    ]
    Alamofire.upload(multipartFormData: { MultipartFormData in
        for (key, value) in body {
            MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
        }
    }, usingThreshold: UInt64.init(),
       to: "URL",
       method: .post,
       headers: ["Authorization": "Your access token"], // As per the web service requirement
       encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.responseJSON { response in

            }
        case .failure(let error):
            print(error)
        }
    })

and see if it works.

Rachit Agarwal
  • 356
  • 2
  • 9
  • it kind of work I am getting message = "Required request part 'payload' is not present"; – Nouf Apr 24 '19 at 07:14
  • In that case try to convert body dictionary into JSON string and pass it to payload like let payload = [ "payload": {"id": "101","message": test,"type": "test"} ] and change the for-in loop to for (key, value) in payload { MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key) } – Rachit Agarwal Apr 24 '19 at 13:25
  • that the part I am struggling with convert the dictionary into JSON , I have no clue how it can be done – Nouf Apr 24 '19 at 13:48
  • if let jsonData = try? JSONSerialization.data ( withJSONObject: bodyDictionary, options: [] ), let jsonString = String(data: jsonData, encoding: String.Encoding.utf8) { print(“JSON String is: \(jsonString)”) } – Rachit Agarwal Apr 24 '19 at 16:10