2. Posting Data over HTTP
When you post data in using HTTP, you have 2 content type
options:
- multipart/form-data
- application/x-www-form-urlencoded
Both formats transfer plain text over http.
The main difference is how the data is formatted.
3. Posting Data over HTTP
The default format used when posting data is x-www-
form-urlencoded.
There is a common misconception that form-data is the
default format.
4. Format of post using x-www…
x-www-form-urlencoded posts data using a format
similar to (actually the same as) url parameters. This
is just a standard, the body can contain data in any
format as long as your backend is setup to handle it.
POST /my-api/customer/address/ HTTP/1.1
Host: my-server.com.au
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
NewShipping%5BfirstName%5D=Kiwi&NewShipping%5BlastName%5D=Jones&NewShipping%5Baddress1%5D=
Some+Place&NewShipping%5Baddress2%5D=In+New+Zealand&NewShipping%5Btown%5D=1842%2C+Surfdale
%2C+Waiheke+Island&NewShipping%5Bphone%5D=12345678&NewShipping%5Bcountry%5D=171&ShippingAd
dressOption=1&NewShipping%5Bgender%5D=male&NewShipping%5BbirthdayDay%5D=1&NewShipping%5Bbi
rthdayMonth%5D=2&NewShipping%5BbirthdayYear%5D=1942
5. Format of post using form-
data Form-data posts data using very structured data format. The body must be in this
format.
It’s “multipart” because each section can have it’s own headers allowing for different
types of data to be transmitted.
POST /my-api/customer/address/ HTTP/1.1
Host: my-server.com.au
Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="products[0][p]"
AT049AA59TWQ
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="products[0][image]”; Content-Type: image/png;
filename=“product_image.png”;
<base64 encoded image>
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="products[0][gift_wrap_num]"
0
----WebKitFormBoundaryE19zNvXGzXaLvS5C
6. Images and Password
Base64 encoded.
Plain HTTP username and password are base 64 encoded
and added to the headers.
“username:password” is the format (similar to apache
htaccess.
Key: Authorize
Value: Basic <Base64>
Images, by default are Base64 encoded in the form-data
format.
Content-Type and filename can he optionally specified for
the “part” that contains the image.
7. Uploading Images Using
Volley
Volley by default uses application/x-www-form-
urlencoded format.
Certain services may require a multipart/form-data post.
Simple MultipartRequest for Google Volley
We fallback to using direct HTTP, but what do you do if
you’re using Volley?
8. Uploading images bypassing
Volley
Reuse your HttpStack implementation, create a new
instance of HttpStack and use it to get your HttpClient.
Use the HttpClient to build a multipart post.
Example code here:
https://gist.github.com/alphamu/7190a8ed0986b5fb8376
9. Thank you
MultipartRequest for Google Volley
https://gist.github.com/alphamu/684d8ae311d95831ce1c
Sample SslHttpStack implementation and Uploading
images using it.
https://gist.github.com/alphamu/7190a8ed0986b5fb8376
Learn more about multipart messages:
http://en.wikipedia.org/wiki/MIME#Multipart_message
s