Submitting an array of dictionaries using alamofire

I need to send an array of dictionaries via a POST request. For instance:

materials: [[String, String]] = [
  [
    "material_id": 1,
    "qty": 10
  ],
  [
    "material_id": 2,
    "qty": 5
  ]
]

Alamofire.request sends the following data:

materials => array(
  [0] => array("material_id" => 1),
  [1] => array("qty" => 10),
  [2] => array("material_id" => 2),
  [3] => array("qty" => 5),
)

I want to get this view:

materials => array(
  [0] => array(
    "material_id" => 1,
    "qty" => 10
  ),
  [1] => array(
    "material_id" => 2,
    "qty" => 5
  ),
)
+4
source share
3 answers

The problem was the add method. I encoded in PHP for 5 years and forgot that in Swift indexes are not automatically assigned, as in PHP. So my first listened code:

func getParameters() -> [[String: AnyObject]] {
    var result = [[String: AnyObject]]()

    for mmap in mmaps {
        let material: [String: AnyObject] = [
            "material_id": mmap.material.id,
            "quantity": mmap.qty
        ]
        result.append(material)
    }

    return result
}

The answer rigidly assigns you the keys:

func getParameters() -> [String: [String: AnyObject]] {
    var result = [String: [String: AnyObject]]()

    let mmaps = self.mmaps.allObjects as [Mmap]
    for i in 0..<mmaps.count {
        let mmap = mmaps[i]
        let material: [String: AnyObject] = [
            "material_id": mmap.material.id,
            "quantity": mmap.qty
        ]
        result["\(i)"] = material
    }

    return result
}
+2
source

A few thoughts:

  • It would be easiest if you sent the answer in the form of a dictionary with one key and correctly encoded the array in the dictionary:

    let materials = [ "materials":
        [
            [
                "material_id": 1,
                "qty": 10
            ],
            [
                "material_id": 2,
                "qty": 5
            ]
        ]
    ]
    

    parameters of request(), Alamofire .

  • , - JSON. JSON ( JSONSerialization JSONEncoder), .

  • application/x-www-form-urlencoded , . Swift 3 :

    func encodeParameters(_ object: Any, prefix: String? = nil) -> String {
        if let dictionary = object as? [String: Any] {
            return dictionary.map { key, value -> String in
                self.encodeParameters(value, prefix: prefix != nil ? "\(prefix!)[\(key)]" : key)
                }.joined(separator: "&")
        } else if let array = object as? [Any] {
            return array.enumerated().map { (index, value) -> String in
                return self.encodeParameters(value, prefix: prefix != nil ? "\(prefix!)[\(index)]" : "\(index)")
            }.joined(separator: "&")
        } else {
            let escapedValue = "\(object)".addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed)!
            return prefix != nil ? "\(prefix!)=\(escapedValue)" : "\(escapedValue)"
        }
    }
    

    extension CharacterSet {
    
        /// Returns the character set for characters allowed in the individual parameters within a query URL component.
        ///
        /// The query component of a URL is the component immediately following a question mark (?).
        /// For example, in the URL `http://www.example.com/index.php?key1=value1#jumpLink`, the query
        /// component is `key1=value1`. The individual parameters of that query would be the key `key1`
        /// and its associated value `value1`.
        ///
        /// According to RFC 3986, the set of unreserved characters includes
        ///
        /// `ALPHA / DIGIT / "-" / "." / "_" / "~"`
        ///
        /// In section 3.4 of the RFC, it further recommends adding `/` and `?` to the list of unescaped characters
        /// for the sake of compatibility with some erroneous implementations, so this routine also allows those
        /// to pass unescaped.
    
        static var urlQueryValueAllowed: CharacterSet = {
            let generalDelimitersToEncode = ":#[]@"    // does not include "?" or "/" due to RFC 3986 - Section 3.4
            let subDelimitersToEncode = "!$&'()*+,;="
    
            var allowed = CharacterSet.urlQueryAllowed
            allowed.remove(charactersIn: generalDelimitersToEncode + subDelimitersToEncode)
            return allowed
        }()
    }
    

    , response (, response vs. responseJSON vs....).

    , , :

    materials[0][material_id]=1&materials[0][qty]=10&materials[1][material_id]=2&materials[1][qty]=5
    

    , -, .

, application/x-www-form-urlencoded /, . , , , RFC, . JSON.

Swift . .

+2

JSON , JSON , , lke this:

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject: yourArry options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

, ..:)

0
source

Source: https://habr.com/ru/post/1570123/


All Articles