Alamofire: Send JSON with Array of Dictionaries

前端 未结 3 1437
一整个雨季
一整个雨季 2020-12-10 08:04

I have a data structure that looks like this in JSON:

[{
    \"value\": \"1\",
    \"optionId\": \"be69fa23-6eca-4e1b-8c78-c01daaa43c88\"
}, {
    \"value\":         


        
3条回答
  •  死守一世寂寞
    2020-12-10 08:57

    You can provide parameter encoding for JSON POST request and it will send the data as JSON in request body.

    Alamofire.request(.POST, "https://httpbin.org/post", parameters: parameters, encoding: .JSON)
    

    This is described in the ReadMe file of Alamofire github - https://github.com/Alamofire/Alamofire#post-request-with-json-encoded-parameters

    let parameters = [
        "foo": [1,2,3],
        "bar": [
            "baz": "qux"
        ]
    ]
    
    Alamofire.request(.POST, "https://httpbin.org/post", parameters: parameters, encoding: .JSON)
    // HTTP body: {"foo": [1, 2, 3], "bar": {"baz": "qux"}}
    

提交回复
热议问题