From the course: Introducing Postman

Build a request body - Postman Tutorial

From the course: Introducing Postman

Start my 1-month free trial

Build a request body

- [Instructor] Query string parameters, headers, and cookies are some of the ways that we can send data to an API, but we'll often find ourselves needing to send data via a request body as well. Let's look at building a request body to get specific results from our sample API. We'll again begin by opening a new request tab and entering a URL. We'll have the same base URL. We're going to go against the boards route, and we're going to use its search. To include a request body, we must also change the HTTP Method from GET to one that allows a body. In this case, the API specifies that we use POST. So just as the response body, we've seen so far, contains the response body, the request body tab is where we enter the request body. So click over to the request body tab. You'll see that the body is currently set to none. To provide a request body, we need to select the format that the server expects. Different APIs have different requirements. For instance, a simple email form or file upload might expect data as form fields. In those cases, we need to select the form data option. Our API doesn't use this approach but let's look at the options anyway. The form data editor should be very familiar by this point. One interesting difference for the form data grid is that when we add an item to the grid, we're asked to choose what type of data we're sending. By default, the text option is selected so we can enter some text in the value column But when we select the file option instead, something interesting happens. See how the value column changed and now includes a select file button? This provides us with an easy way to select a file to send to the server. This might be useful for user profile images or something like that. The XWWW form URL encoded and binary options allow us to do similar things by entering form data values, or uploading files respectively. Now again, we're not using the form data so we can delete this row. Instead of form data, we'll be sending JSON data describing our search options. For that we need to select the Raw option. Rather than seeing the familiar grid, we now have only a text area where we enter the request body directly. Our search definition is pretty straightforward but it will require a little bit of typing. Before we begin though, there's one more thing we should do. and that is set the content type. We could do that by switching over to the header's tab and defining the header directly, but Postman offers an easier way. Immediately to the right of the body options, is a dropdown list. The currently selected value is text, implying that we're sending raw text but we're not. We're sending JSON data. Choose the JSON option from the list. Now, let's switch over to the header's tab, and see that Postman has automatically set the value for us. Having confirmed the change, let's go back to the body tab, and now we'll build our search definition. The search criteria is just a basic JSON object with a few attributes. The first attribute is filters. This tells the API what we want to search on. It's an array that contains objects which define what we want to search for, field of description, value of management. We're saying we want to search the database for any board with the word management in it's description. Let's also apply some sort options by providing the sort attribute. And I'm just going to sort by name. So one advantage of selecting the proper content type is that several of them allow easily formatting the request body. Here we have chosen JSON so the Beautify option appeared. Click that, and Postman will reformat the content to a more typical style. Now, send the request and you'll see two results come back. If by chance you see an HTML document returned instead of the two results, be sure that you've included all punctuation in the request body including quotes, commas, and brackets. This process of defining a request body is the same regardless of whether we're searching, creating new objects, or modifying existing objects. So it's important to practice these techniques. I encourage you to experiment with the search and see what happens.

Contents