spring-bootalamofireswift5xcode11alamofire-request

Alamofire 5 POST method having array parameters usage with swift 5


I am working on an iOS project with Swift 5 with Almofire 5.0.2. I tried all the options suggested on an online thread similar to this but it looks like I am still missing something.

I am facing difficulty in during a POST method API call with an array of items as apart of the request body.

API looks like:

@RequestMapping(...)
SomeResponse doSomething (@RequestBody SomeRequest request) {}

I am using Swift 5 and alamo fire 5.0.2 version and I tried above option along with below option but it didn't hit API at all( returns AFError):

let parameters: [String: Any] = [
            "block": true,
            "items": [],
            "name": "xxxxx",
            "mobile": "xxxxxxxxxx",
            "email": "xxxx@gmail.com",
            "List": [["place": "london"],["place": "holand"]]

  ]

AF.request(BookingConstants.httpEndpoint, method: .post, parameters: parameters, headers: headers)
            .validate()
            .responseData { response in

}

I am getting this error on java spring boot API side :

Invalid index in property path 'item[][place]'; nested exception is java.lang.NumberFormatException: For input string: ""] with root cause

java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[?:1.8.0_221]
    at java.lang.Integer.parseInt(Integer.java:592) ~[?:1.8.0_221]
    at java.lang.Integer.parseInt(Integer.java:615) ~[?:1.8.0_221]
    at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:663) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyHoldingValue(AbstractNestablePropertyAccessor.java:402) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.AbstractNestablePropertyAccessor.processKeyedProperty(AbstractNestablePropertyAccessor.java:298) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:289) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:280) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:859) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.validation.DataBinder.doBind(DataBinder.java:755) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:192) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.bind.ServletRequestDataBinder.bind(ServletRequestDataBinder.java:106) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE

I know I am missing something but couldn't figure it out. Can someone please help me out with this? Please let me know your comments/suggestions if any. Thanks in advance


Solution

  • I switched back to Alamofire 4.0.1 version and used the Alamofire upload method to achieve this.

    let objectAsJSON: JSON = request.toJSON()
    let data: Data = try objectAsJSON.serialize()
    
    Alamofire.upload(data, to: BookingConstants.httpEndpoint, headers: headers)
                    .validate()
                    .responseData { response in
            // response code logic
    
    }