0

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

  • You need to post the error you're getting. – Jon Shier Apr 05 '20 at 21:16
  • @JonShier Thanks ! i have updated logs – VIKRAM SINGH CHOUHAN Apr 06 '20 at 21:21
  • I meant the client error. But it sounds like you're sending bad data, so the server is likely responding with a 500 error which doesn't contain JSON. – Jon Shier Apr 07 '20 at 02:29
  • @JonShier: Thanks for sharing your thoughts. There is no error on the client-side as it is a bad request and as u said I am not sending the data in the right format. That is where I am stuck what is the right way to send this kind of data from iOS client. Maybe i have to switch to some other alternative of almofire. – VIKRAM SINGH CHOUHAN Apr 08 '20 at 06:37
  • I'm not sure why an alternative would help if you don't know what your parameters are supposed to look like. What kind of parameter encoding does the server expect? – Jon Shier Apr 08 '20 at 15:35

1 Answers1

0

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

}