A POST request is used to modify the requested resource on a server.

HTML Forms with File Uploads

The HTML for a form that includes a file upload is slightly different. Specifically, there are two changes:

  1. An input element with the type “file” is added, and
  2. The encryption type of the form element must be set to “multipart/form-data”

<form method=”post” action=”/update_profile” enctype=”multipart/form-data”> <input type=”text” name=”handle”> <input type=”file” name=”avatar”> <button type=”submit”>Upload</button> </form>

When the form is submitted, the request looks something like the following:

POST /update_profile HTTP/1.1

Content-Type: multipart/form-data; boundary=—-TH23XJ

Content-Length: 34209

——TH23XJ Content-Disposition: form-data; name=”handle” treehouse

——TH23XJ Content-Disposition: form-data; name=”avatar”; filename=”me.jpg” Content-Type: image/jpeg

[binary data of file] ——TH23XJ

Notice that the data is transmitted in parts, which explains the “multipart/form-data” encryption type of the form, as well as the resulting and identical content-type header.

For more practice go to type httpbin.org and play with chrome developer tools.

The “method” attribute is used in an HTML form tag to trigger a POST request upon submission.



We're not around right now. But you can send us an email and we'll get back to you, asap.


©2021 RussellCoder

Log in with your credentials

Forgot your details?