Search code examples
iosswiftalamofire

Swift Alamofire multipartFormData.appendBodyPart Bool?


i am currently stuck with my schoolproject at uploading an image with parameters and headers. The problem is that i don't know how to add Bools to the multipartFormData. Does anyone know?

multipartFormData.appendBodyPart(
                    data: self.house.image!,
                    name: "file",
                    fileName: "testIMG.png",
                    mimeType: "image/png"
                )
                 multipartFormData.appendBodyPart(data:self.house.streetName!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"streetName")
                multipartFormData.appendBodyPart(data:self.house.streetNumber!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"streetNumber")
                multipartFormData.appendBodyPart(data:self.house.city!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"city")
                multipartFormData.appendBodyPart(data:self.house.zipcode!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"zipcode")
                multipartFormData.appendBodyPart(data:self.house.kitchen_shared!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"kitchen_shared")
                multipartFormData.appendBodyPart(data:self.house.livingroom_shared!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"livingroom_shared")
                multipartFormData.appendBodyPart(data:self.house.bathroom_shared!, name :"bathroom_shared")
},

The kitchen_shared, livingroom_shared and bathroom_shared are the Bools i am stuck with.


Solution

  • Send the value as string as any parameter is a string in http request body. Catch the variable @backend and parse it to boolean (it depends on your backend implementation).

    multipartFormData.appendBodyPart(data:self.house.livingroom_shared.description.dataUsingEncoding(NSUTF8StringEncoding)!,name:"kitchen_shared")