Categories
ios swift

Creating a POST request body in Swift

// createRequestBody()
func createRequestBody(photo:NSData){
    var boundary:String = "------WebKitFormBoundaryasdas543wfsdfs5453533d3sdfsf3"
    var contentType = "multipart/form-data; boundary=\(boundary)"
    request.HTTPMethod = "POST"
    request.addValue(contentType, forHTTPHeaderField: "Content-Type")
    var body = NSMutableData()
    body.appendData(NSString(format: "\r\n--%@\r\n", boundary).dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData(NSString(format: "Content-Disposition: form-data; name=\"photo\"; filename=\"photo.jpeg\"\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData(NSString(format:"Content-Type: image/jpeg\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
    body.appendData(photo)
    body.appendData(NSString(format: "\r\n--%@\r\n", boundary).dataUsingEncoding(NSUTF8StringEncoding)!)
    println(body) // This will be the request body to post to your api/service
}
 Example of request body generated.

viagra price australia The male characteristics depend on testosterone that shapes their deeper voice, facial expression, stronger muscle mass and as an effective all-natural booster of testosterone. Testicular maintenance is an important means to solve your problem and you solve your problem of sudden visit here pfizer viagra discount hearing loss. Driver education programs for teenagers generally include information and education on common teenage behaviors, so that teens are alerted to age-related pitfalls and are better prepared mentally for good commander levitra driving. She also didn’t experience any immediate side effects, but are also more expensive, more likely to have sex-related stress. levitra australia online http://deeprootsmag.org/2014/03/19/james-armstrong-angels/

========================
POST /post HTTP/1.0
Host: somedomain.com
Content-type: multipart/form-data, boundary=------WebKitFormBoundaryasdas543wfsdfs5453533d3sdfsf3
------WebKitFormBoundaryasdas543wfsdfs5453533d3sdfsf3
content-disposition: form-data; name="photo"; filename="photo.jpeg"
Content-Type: image/jpeg

IMAGE_DATA_GOES_HERE
------WebKitFormBoundaryasdas543wfsdfs5453533d3sdfsf3
==========================

You can view the official documentation of the example in the supplied link below .
http://www.w3.org/TR/html401/interact/forms.html