2

How to send this parameter to multipart

    let dictionary = [
        "user" :
            [
                "email" : "\(email!)",
                "token" : "\(loginToken!)"
        ],
        "photo_data" :[
            "name" : "Toko Tokoan1",
            "avatar_photo" : photo,
            "background_photo" : photo,
            "phone" : "0222222222",
            "addresses" :[[
                "address" : "Jalan Kita",
                "provinceid" : 13,
                "cityid" : 185,
                "postal" : "45512"
                ]],
            "banks" :[[
                "bank_name" : "PT Bank BCA",
                "account_number" : "292993122",
                "account_name" : "Tukiyeum"
                ]]

        ]

    ]

I tried this below code but I can't encode value (which in NSDic) to utf 8

        for (key, value) in current_user {
            if key == "avatar_photo" || key == "background_photo"{
            multipartFormData.appendBodyPart(fileURL: value.data(using: String.Encoding.utf8)!, name: key) // value error because its NSDic
            }else{
             multipartFormData.appendBodyPart(data: value.data(using: String.Encoding.utf8)!, name: value) // value error because its NSDic
            }

        }

value in append body part cannot be use because it's NSDictionary not string. How the right way to put that parameter in multipartformdata?

1 Answer 1

1

It is allowed to have nested multiparts.

The use of a Content-Type of multipart in a body part within another multipart entity is explicitly allowed. In such cases, for obvious reasons, care must be taken to ensure that each nested multipart entity must use a different boundary delimiter.

RFC 1341

So you have to do the same you did on the outer loop: Simply loop through the contents of the dictionary generating key-value pairs. Obviously you have to set a different part delimiter, so the client can distinguish a nested part change from a top-level part change.

Maybe it is easier to send the whole structure as application/json.

Sign up to request clarification or add additional context in comments.

1 Comment

Maybe it is easier to send the whole structure as application/json. Yes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.