Search code examples
jsonswiftalamofire

Response not coming from Alamofire post method servicecall in swift


when i give same parameters i am getting response in postman like this

enter image description here

code: why i am not getting response like postman in this makeMultipartRequest() call? where am i wrong? how to get response and error while parsing.

func makeMultipartRequest() {
    let url = "https://test.com/SaveUndertakingAckowledgement"
    
    let parameters: [String: Any] = [
        "SchCode": "Test",
        "key": "ndjsfnjkds==",
        "UserID": 11,
        "UserType": 3,
        "UtID": "kfnksdlnfks"
    ]
    
    Alamofire.upload(multipartFormData: { multipartFormData in
        for (key, value) in parameters {
            if let data = "\(value)".data(using: .utf8) {
                multipartFormData.append(data, withName: key)
            }
        }
    }, to: url) { result in
        switch result {
        case .success(let upload, _, _):
            upload.responseJSON { response in
                switch response.result {
                case .success(let value):
                    print("Response JSON: \(value)")
                    // Handle success response
                case .failure(let error):
                    print("Error: \(error)")
                    // Handle error
                }
            }
        case .failure(let error):
            print("Error: \(error)")
            // Handle error
        }
    }
}

func makePostRequest() {
    let url = "https://test.com/SaveUndertakingAckowledgement"
    
    let parameters: [String: Any] = [
        "SchCode": "test",
        "key": "nsjdkfn--=",
        "UserID": 11,
        "UserType": 3,
        "UtID": "ewiw/D264"
    ]
    
    Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: nil)
        .responseJSON { response in
            switch response.result {
            case .success(let value):
                print("Response JSON only post...: \(value)")
                // Handle success response
            case .failure(let error):
                print("Error only post...: \(error)")
                // Handle error
            }
        }
}

Response JSON:

{
    Message = "No HTTP resource was found that matches the request URI 'https://gfvhv.test.com/SaveUndertakingAckowledgement'.";
    MessageDetail = "No action was found on the controller 'User' that matches the request.";
}

Solution

  • Regarding your Postman screen, you need do a "simple" POST (no multiform part) and put the parameters in the URL (while for a POST it's usually in the httpBody).

    The Alamofire solution would be then:

    AF.request(url,
               method: .post,
               parameters: parameters,
               encoding: URLEncoding(destination: .queryString),
               headers: nil)  //Put your headers there
    

    With URLEncoding(destination: .queryString) you should be able to tell to put the parameters inside the URL (ie as the query).

    Side note: You can generate cURL with POSTMAN, and you can generate cURL with Alamofire. It's can be really helpful to have a common ground for comparison and trying to match the working solution.

    In Alamofire, you just need:

    Alamofire.request(...)
             .cURLDescription { 
                 print("AF cURL generated:\($0)")
             }
             .response(...) //or .responseJSON(), but it's deprecated, prefers other ones
    

    In your case, I got:

    $ curl -v \
        -X POST \
        -H "Accept-Encoding: br;q=1.0, gzip;q=0.9, deflate;q=0.8" \
        -H "User-Agent: ... Alamofire/5.4.0" \
        -H "Accept-Language: en-US;q=1.0" \
        "https://test.com/SaveUndertakingAckowledgement?SchCode=Test&UserID=11&UserType=3&UtID=kfnksdlnfks&key=ndjsfnjkds%3D%3D"
    

    Except for the User-Agents, Accept-Language, Accept-Encoding which are more specific to the app/device and are usually not problematic, we see that the parameters have been added to the URL.