-1

I have a problem using swift because I need to send a Json like this:

 {
   "package": {
     "description": "Pink iPad",
     "contentValue": 120.01,
     "weight": 1.01,
     "length": 30.01,
     "height": 15.01,
     "width": 20.01
   },
   "origin_zip_code": "44100",
   "destination_zip_code": "44510"
 }
 

That's my attempt to do that:

   let package : [String: Any?] = [
    "description": "Pink iPad",
        "contentValue": 120.0,
        "weight": 1.01,
        "length": 30.01,
        "height": 15.01,
        "width": 20.01
    ]

    
     parameters = [
        "package": package,
        "origin_zip_code": "44100",
        "destination_zip_code": "44510"
    ]

When I got a response from Alamofire I have the following message:

failure(Alamofire.AFError.responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})))

What's wrong with it? Thank you so much for reading

Antonio Labra
  • 1,694
  • 2
  • 12
  • 21
  • 1
    You can print your parameters before sending the request, and at a quick glance it appears to be correct. Take a look at the api and test the request in postman, whats the response and format. – valosip Jan 07 '21 at 18:57
  • are you trying to encode to send or decode the response? Your code seems suggest you re encoding, and error suggests you are decoding – timbre timbre Jan 07 '21 at 20:11

1 Answers1

0

You could use Parameters Object:

 let package: Parameters = [
            "description" : "Pink iPad",
                "contentValue": 120.0,
                "weight": 1.01,
                "length": 30.01,
                "height": 15.01,
                "width": 20.01
        ]

let parameters : Parameters = [
            "origin_zip_code": "44100",
            "destination_zip_code": "44510",
            "package": package
        ]
timbre timbre
  • 12,648
  • 10
  • 46
  • 77
Antonio Labra
  • 1,694
  • 2
  • 12
  • 21