-1

enter image description hereenter image description hereenter image description hereI am using Alamofire. I am stuck in posting the post request. I have a post body which is -

[
    {
    "siteName": "lab1",
    "locationCode": "111",
    "locationName": "test1"
    }
]

How should I make the request call? I am doing -

    let parameters: Parameters = [
        "siteName": "lab",
        "locationCode": "1156",
        "locationName": "123test"
    ]
    Alamofire.request(URLStr, method: .post, parameters: parameters , encoding: JSONEncoding.default, headers: headers).responseJSON { response in
        print("Request: \(String(describing: response.request))")   // original url request
        print("Response: \(String(describing: response.response))") // http url response
        print("Result: \(response.result)")                         // response serialization result

        if let json = response.result.value {
            print("JSON: \(json)") // serialized json response
            sucessHandler(json)
        }

        if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
            print("Data: \(utf8Text)") // original server data as UTF8 string
            failureHandler(response.error)
        }
    }
  • 1
    What is issue ? – Prashant Tukadiya Sep 20 '17 at 11:09
  • 1
    In your code there is one typo mistake `parameters: params` should be `parameters: parameters` in `Alamofire.request` – Prashant Tukadiya Sep 20 '17 at 11:10
  • What is the status code you're getting back? you can print it using `response.response?.statusCode` – mfaani Sep 20 '17 at 11:12
  • Its giving following error after hitting the server-- json value ["status": 400, "exception": org.springframework.http.converter.HttpMessageNotReadableException, "message": JSON parse error: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: java.io.PushbackInputStream@2104e118; line: 1, column: 1], "timestamp": 1505905556480, "path": /Configuration/saveStoreLocationDetails/, "error": Bad Request] – Nimisha Khandelwal Sep 20 '17 at 11:13
  • Looks like backhand team issue , is same working on PostMan ? – Prashant Tukadiya Sep 20 '17 at 11:15
  • check your URL "status": 400 – Salman Ghumsani Sep 20 '17 at 11:16
  • yes its working in postman. I think the json body which I am sending is not matching with the request body? Could you please check this? – Nimisha Khandelwal Sep 20 '17 at 11:16
  • 1
    Did you check `params` object ? it should be `parameters` – Prashant Tukadiya Sep 20 '17 at 11:19
  • By the way you need to pass array of dictionary !! like `[parameters]` in the Request parameter, As your error suggest – Prashant Tukadiya Sep 20 '17 at 11:20
  • several notes: 1) 400 means "The 400 Bad Request error is an HTTP status code that means that the request you sent to the website server, often something simple like a request to load a web page, was somehow incorrect or corrupted and the server couldn't understand " so likely you're passing something wrong and **it's not a backend issue**. 2) Maybe your encoding is incorrect. What version of Swift and Alamofire are you using? – mfaani Sep 20 '17 at 11:21
  • @JonSnow no he should just pass `parameters`. No need to put it inside an array. @user see [this answer](https://stackoverflow.com/a/40219394/5175709) – mfaani Sep 20 '17 at 11:22
  • heyy Jon - I didnt get this (By the way you need to pass array of dictionary !! like [parameters] in the Request parameter, As your error suggest). – Nimisha Khandelwal Sep 20 '17 at 11:23
  • user PLEASE CONFIRM that you've changed to `parameters` and you're not using `params`. Once you fixed that, please EDIT the question. YOU HAVE TO RESPOND so we know you're making the changes – mfaani Sep 20 '17 at 11:24
  • @Honey Actually it need to , I might wrong but if you observe first block **I have a post body which is -** it is array of dictionary ? doesn't it ? – Prashant Tukadiya Sep 20 '17 at 11:25
  • @user1947951 Please try `Alamofire.request(URLStr, method: .post, parameters:[ parameters] , encoding: JSONEncoding.default, headers: headers).responseJSON { response in` and let me know it is working or not – Prashant Tukadiya Sep 20 '17 at 11:27
  • @JonSnow just see their gitHub page [here](https://github.com/Alamofire/Alamofire). I only see them using `parameters` – mfaani Sep 20 '17 at 11:28
  • Yes i HAVE changed PARAMS into parameters and not usng params – Nimisha Khandelwal Sep 20 '17 at 11:31
  • @user1947951 so please edit the question, so others coming here would know... – mfaani Sep 20 '17 at 11:31
  • @user1947951 it might be that your backend can't accept JSON-Encoding and can only accept URL-Encoding. Just try changing your encoding to `encoding: URLEncoding.default` For more, see their examples on their [GitHub page](https://github.com/Alamofire/Alamofire) – mfaani Sep 20 '17 at 11:32
  • @user1947951 check this answer https://stackoverflow.com/a/43857745/4601900 – Prashant Tukadiya Sep 20 '17 at 11:34
  • I have tried this -- Alamofire.request(URLStr, method: .post, parameters:[ parameters] , encoding: JSONEncoding.default, headers: headers).responseJSON . - but the compiler says extra argument method in call. – Nimisha Khandelwal Sep 20 '17 at 11:38
  • encoding to encoding: URLEncoding.default tried this also :( – Nimisha Khandelwal Sep 20 '17 at 11:39
  • can you take a screenshot of what you're doing in postman? Take a screenshot of all pages where you add a value? (blur out whatever you can't show...). I also downvoted since you didn't make an edit yet. – mfaani Sep 20 '17 at 11:47
  • check this answer stackoverflow.com/a/43857745/4601900 -- doesn't work – Nimisha Khandelwal Sep 20 '17 at 12:03

2 Answers2

2

Thank you so much guys. I found the other way of doing this.

let fileUrl = NSURL(string: URLStr)

    var request = URLRequest(url:fileUrl as! URL )
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")

    let values = [parameters]

    request.httpBody = try! JSONSerialization.data(withJSONObject: values)

    Alamofire.request(request)
        .responseJSON { response in
            // do whatever you want here
            switch response.result {
            case .failure(let error):
                print(error)

                if let data = response.data, let responseString = String(data: data, encoding: .utf8) {
                    print(responseString)
                    failureHandler(response.error)

                }
            case .success(let responseObject):
                print(responseObject)
                do {
                    let user = try IeroLocationSave(JSONDecoder(response.data ?? "nil..12"))
                    //print("city is: \(user.address.city)")
                    sucessHandler(user)
                    //That's it! The object has all the appropriate properties mapped.
                } catch {
                    print("unable to parse the JSON")
                }

            }
    }
0

Below code works for me

let parameters: Parameters = ["feedback_name": "SwiftTest","feedback_email":"m@m.com","feedback_description":"Test"]

        Alamofire.request("http://212.69.45.77:8082/api/feedbackapp",method: .post,parameters: parameters).responseJSON { response in
            print("Request: \(String(describing: response.request))")   // original url request
            print("Response: \(String(describing: response.response))") // http url response
            print("Result: \(response.result)")                         // response serialization result

            if let json = response.result.value {
                print("JSON: \(json)") // serialized json response
            }

            if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
                print("Data: \(utf8Text)") // original server data as UTF8 string
            }
        }