0

I'm trying to post following JSON to the api. Following is the log from the Xcode console.

{
    address = (
        {
            city = 1;
            area = 1;
            "building_name" = "building";
        }
    );
    category = 1;
    inspection = 0;
    subCategory = (
        12
    );
}

So here address field is array of nested JSON objects. The problem is that address field isn't getting properly in server side. I think it thinks that sub fields including city, area, etc as individual dictionaries not a dictionary as a whole. Following is the log from the Laravel.

array (
    'address' => 
    array (
        0 => 
        array (
           'city' => '1',
        ),
        1 => 
        array (
           'area' => '1',
        ),
        2 => 
        array (
           'building_name' => 'building',
        ),
    ),
    'category' => '1',
    'inspection' => '0',
    'subCategory' => 
    array (
        0 => '12',
    ),
)

Basically I'm wondering if somehow Alamofire is trying to jsonify them twice but have no way to avoid it. I'm using Alamofire 4.7.2 and Gloss 2.0 and server side is based on Laravel.

Pei
  • 11,452
  • 5
  • 41
  • 45
  • What's your code? Why does it seems to convert Dictionary as Array? – Larme May 05 '18 at 11:30
  • Updated the answer. Basically those are from the consoles of the Xcode and Laravel respectively. @Larme – Pei May 05 '18 at 11:33

2 Answers2

0

Whatever you print out it is most definitely not JSON. Try feeding your Alamofire methods with Codable objects, this might look as follows (in a Playground):

import Cocoa

struct Address: Codable {
    let city: Int
    let area: Int
    let building_name: String
}

struct Inspection : Codable {
    let address: Address
    let category: Int
    let inspection: Int
    let subCategory: [Int]
}

let inspection = Inspection(address: Address(city: 1, area: 1, building_name: "building"), category: 1, inspection: 0, subCategory: [12])

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted

do {
    let jsonData = try encoder.encode(inspection)
    print(String(data: jsonData, encoding: .utf8)!)
} catch {
    print(error)
}

This will print out the following JSON:

{
    "inspection" : 0,
    "subCategory" : [
    12
    ],
    "category" : 1,
    "address" : {
        "building_name" : "building",
        "city" : 1,
        "area" : 1
    }
}

roughly in line with what you provided above. Not sure what subCategory was supposed to be so I took a guess. The beauty of this approach is that you are given a free JSON-parser through JSONDecoder which is really awesome. Try to build from here (or tell us what your output is supposed to represent).

Patru
  • 4,481
  • 2
  • 32
  • 42
0

It was because I was using URLEncoding so the server side couldn't parse the parameters properly. Switched to JSONEncoding and it just worked fine.

So use

JSONEncoding.default.encode(urlRequest, with: params)

instead of

URLEncoding.queryString.encode(urlRequest, with: params)
Pei
  • 11,452
  • 5
  • 41
  • 45