AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Line breaks are also important when building a HTTP body such the above one. See how boundary indicates the beginning of each part and also see how semicolon (“ ”) separates attributes in headers. At first, the “multipart/form-data” content type is specified along with the boundary string that separates the data parts. Notice how everything mentioned in the previous paragraphs is used. The following is a fake example of a HTTP request body that uses the “multipart/form-data” content type: When appending files data, the filename should be also included in the “Content-Disposition” header with the original name of the file, as well as the content type (MIME type) of each file that is about to be uploaded. These two key-value pairs are enough when adding single data (meaning no files) to the request’s HTTP body. Notice that names don’t need to be unique, and sometimes server sets the rules that apply to the “name” attribute. An attribute called “name” should also be provided in the header, as it specifies the name of the part. –abc123).Įach part of a multipart body necessarily starts with a Content-Disposition header, with the form-data value coming in pair with it. Usually, boundaries start with multiple dashes, and then they have an alphanumeric suffix (e.g. It always starts with two dashes (“–“), with an arbitrary combination of other alphanumeric characters coming afterwards. That string should not occur in the actual data, so it must be as much unique as possible. For that purpose, a special and unique string is provided along with the content type, called boundary. Since posted data is broken into parts, it’s necessary for the server to know where a part starts and where it ends. “Multipart/form-data” content type tells to HTTP request that posted data should be broken into parts, as if they were to be posted by a web form that expects from users to fill in various fields and select files that should be submitted to a server. This content type allows to send files or large amounts of data in combination with other usual data that should be posted. Let me start by saying that in order to upload files to a server, multipart/form-data is the content type that should be specified in the web request. A Quick Intro To “Multipart/form-data” Content Typeīefore we start doing actual work, it’s necessary some important things to be mentioned first. Before we get there though, it’s necessary to have a quick discussion about some ground knowledge that we all should have on this topic. In the implementation steps that follow we will try to break things down and shed light to the key points and the details of the uploading process. However, it can be proved to be a tedious task to perform when it’s time to send files to a server. Uploading files might not be one of the most common things when dealing with web services. Today, we will keep building on it, as I would like to focus on a specific use case: How to upload files to a server! Welcome to a new, hopefully exciting tutorial! In a previous post I showed to you the process of creating a custom class that manages web requests and RESTful APIs.
0 Comments
Read More
Leave a Reply. |