Uploading an image to a flask server using Swift's NSURLSession
<p>So I cannot get Flask to receive an image I sent from my iOS code (keep getting 400 error on flask). I'm not entirely sure the problem is coming from the iOS side or the flask side but I suspect that it may be because the image is not properly encoded on the iOS side. </p>

<p>Swift code:</p>

<pre><code>func uploadImage(image: UIImage) -&gt; Void{
    //Convert the image to a data blob
    guard let png = UIImagePNGRepresentation(image) else{
        print("error")
        return
    }

    //Set up a network request
    let request = NSMutableURLRequest()
    request.HTTPMethod = "POST"
    request.URL = NSURL(string: "http://127.0.0.1:5000/")
    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
    request.setValue("\(png.length)", forHTTPHeaderField: "Content-Length")
    request.HTTPBody = png
    // Figure out what the request is making and the encoding type...

    //Execute the network request
    let upload = NSURLSession.sharedSession().uploadTaskWithRequest(request, fromData: png) { (data: NSData?, response: NSURLResponse?, error: NSError?) -&gt; Void in
        //What you want to do when the upload succeeds or fails
    }

    upload.resume()

}
</code></pre>

<p>Flask code: </p>

<pre><code>@app.route('/')
def projects():
    if request.method == "POST":
    file = request.files['file']
    if file:
         img = Image.open(file)
         img.show()
return render_template("home.html")
</code></pre>

<p>Thanks!</p>