Aria
Aria

Reputation: 403

Pass an integer as a parameter in an Alamofire PUT request

So I am trying to do a PUT request using Alamofire and I need an integer parameter (not an object). The request sends an id to the database and the query makes an update to an object with that id in the database. The Parameters object in alamofire seems to take only objects:

var parameters: Parameters = ["key" : "value"]

is there any way to just send an integer without using the object? The error I keep getting when I use that method above is:

nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of int out of START_OBJECT token

and I am assuming this means I am passing an object when I should be passing an int instead.

This is my request :

Alamofire.request(url, method: .put, parameters: parameters, encoding: JSONEncoding.default, headers: nil).response{ response in
        if response.response?.statusCode == 200 {
            // pass
        }else{
            // fail
        }
        completionHandler((response.response?.statusCode)!)
    }

I can't seem to find any examples that have to do with us online.

Upvotes: 1

Views: 1291

Answers (1)

Nevin Jethmalani
Nevin Jethmalani

Reputation: 2826

If you give more information about where you are sending the request, then I can test my solution to see if it works. But you can try this.

let url = "YOUR_URL"

let yourData = "WHATEVER CUSTOM VAL YOU WANT TO PASS"

var request = URLRequest(url: URL(string: url)!)
request.httpMethod = "PUT"
//request.setValue("application/json", forHTTPHeaderField: "Content-Type") //This is if you want to have headers. But it doesn't look like you need them. 

request.httpBody  = yourData
/*
do {
    request.httpBody  = try JSONSerialization.data(withJSONObject: yourData)
} catch {
    print("JSON serialization failed:  \(error)")
}
*/
Alamofire.request(request).responseJSON {(response) in
    if response.response?.statusCode == 200 {
        print ("pass")
    }else{
        print ("fail")
    }
    completionHandler((response.response?.statusCode)!)

    /*
    switch response.result {
    case .success(let value):
        print ("success: \(value)")
    case .failure(let error):
        print("error: \(error)")
    }
    */
}

Upvotes: 2

Related Questions