0

I'm having a problem on using Alamofire. When I try to post a request using a generic parameters like ["name":"John", "age":"27"] it always succeeds. But, when I try to use a web service that requires parameters and a body-raw for a base64 string I'm not able to get a successful response from the server. Though it succeeds when I use Postman. Does anyone knows how to do this on Alamofire 4? Here is the screenshot of my postman.

postman screenshot

Thank you!

nathan
  • 8,880
  • 4
  • 34
  • 45
Gelo
  • 31
  • 3
  • Could you provide the Swift code that's returning an incorrect response ? – nathan Sep 06 '17 at 03:26
  • Possible duplicate of [POST request with a simple string in body with Alamofire](https://stackoverflow.com/questions/27855319/post-request-with-a-simple-string-in-body-with-alamofire) – nathan Sep 06 '17 at 03:34

2 Answers2

0

@nathan- this is the code that I used. I just assumed that the base64String inside the "let paramsDict" has a key value named "data" though it doesn't have a key name in postman.

let urlString = ApiManager.sharedInstance.formsURL + ApiManager.sharedInstance.mobileFormsImageUpload
        let paramsDict = ["token": token, "fileID":"2", "filename":"images.png", "data": base64String] as [String : Any]

        Alamofire.request(urlString, method: .post, parameters: paramsDict, encoding: URLEncoding.httpBody, headers: [:])
            .responseJSON{ response in

                switch response.result {
                case .success(let data):

                    debugPrint("SUCCESS")


                case .failure(let error):
                    debugPrint("Request Error")

                }
        }
Gelo
  • 31
  • 3
0

I already figured it out. It needs a custom encoding to make it work. All the parameters must be inlined with the url so the base64 string inside the parameter is the only to be encoded. Here is the code that I used.

struct CustomPostEncoding: ParameterEncoding {
func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
    var request = try URLEncoding().encode(urlRequest, with: parameters)

    let base64 = parameters?["data"] as! String
    let finalBase64Format = "\"" + base64 + "\""
    let postData = NSData(data: finalBase64Format.data(using: String.Encoding.utf8)!)
    request.httpBody = postData as Data
    return request
}
}

func uploadImageBase64(){


    let jpegCompressionQuality: CGFloat = 0.9 // Set this to whatever suits your purpose
    if let base64String = UIImageJPEGRepresentation(testIMG, jpegCompressionQuality)?.base64EncodedString() {

        var token = String()
        if let data = UserDefaults.standard.data(forKey: "userProfile"),
            let user = NSKeyedUnarchiver.unarchiveObject(with: data) as? UserProfile{
            token = user.token
        } else {
            print("There is an issue")
        }

        let headers = [
            "content-Type": "application/json"
        ]
        let urlString = "http://localhost/FormsService.svc/Getbase64?filename=test.png&fileID=1151&token=80977580xxx"
        let paramsDict = ["data": base64String] as [String : Any]
        Alamofire.request(urlString, method: .post, parameters: paramsDict, encoding: CustomPostEncoding(), headers: headers)

            .responseJSON{ response in
                print("response JSON \(response.result)")
        }
            .response{ response in
                print("RESPONSE \(response)")
        }

    }
}
Gelo
  • 31
  • 3