D.C.
D.C.

Reputation: 15588

Manually creating a HTTP POST request for file upload

Ultimately what I'm trying to do is manually craft the proper POST body to send a request from a C program (well, Objective-C).

I started by examining the data sent when a user submits a web form with a file attached. I'm hoping somebody can help clear up some initial confusion I'm having:

  1. Suppose the uploaded file is a png file. How is it encoded on client? How does the Web service know how the file was encoded so that it can successfully turn it back into image data. I have a UIImage but I'm trying to figure out how to write it to the body string so that the web server will know what that data means.

  2. The following is some post data that I was examining. It represents a few form variables as well as a simple png file. I have verified that the server did get the request and added my photo to its database. My question is, why can I not see the actual data that represents the encoded image? It does not seem to be anywhere in this post body.

Thanks a lot for the help.

Content-Disposition: form-data; name="user"

someUser
------WebKitFormBoundaryDPIav9ZBBnDSAURS
Content-Disposition: form-data; name="description"

Description of a photo
------WebKitFormBoundaryDPIav9ZBBnDSAURS
Content-Disposition: form-data; name="photo"; filename="simple.png"
Content-Type: image/png


------WebKitFormBoundaryDPIav9ZBBnDSAURS
Content-Disposition: form-data; name="commit"

Create
------WebKitFormBoundaryDPIav9ZBBnDSAURS--

Upvotes: 1

Views: 2115

Answers (1)

Neil Essy
Neil Essy

Reputation: 3607

In the case of the file it will assume raw bytes for the file terminated by \r\n--boundry however you can additionally specify Content-Length and Content-Encoding which can be base64, gzip, base64-gzip and a few other types.

------WebKitFormBoundaryDPIav9ZBBnDSAURS
Content-Disposition: form-data; name="photo"; filename="simple.png"
Content-Type: image/png
Content-Length: 17829
Content-Encoding: base64

H4sIAAAAAAAAC+y9aZPj1pE2+p2/oq4mYmQHLGHfPHJPAAQJEDuJlRhPOLADJPad+LXvx9f3L9wv
F1XVkrqlakm2ZY9HVkdXFXHOQZ5Ensx8MnESxFf/uZTF0xR3fV5Xf/gM/hL67CmuwjrKq/QPn50M
7QuKwukv4M/+893uK/fIyU/b+Kr/w2fZMDS/B8F5nr/Uxy4+RGn8ZViX4PMYkHgm8zLw90Hjfzy6
C8vmi7QLv0zDL0P/62Hh2A91+SNk9y+Dvj4livs8reLu0ydx70ds/MBfn7UkUfETuH+3e3p6+iot
.....

Upvotes: 2

Related Questions