The Message Body
Making use of POST, PUT, and PATCH you need to send the data in the requests body. In this lesson you’ll learn how to do exactly that. Furthermore, it covers the differences between using the data
and the json
parameter to specify the payload.
00:00
POST
, PUT
, and PATCH
pass data through what’s called the message body. It’s different from the query string parameters that we used before. The payload is passed through the parameter data
, and the data
can take inside of it a dictionary, a list of tuples, bytes, or file-like objects.
00:17
And if you want to use JSON, you’d change it from data
to json
, and then requests
will serialize the data for you and add the Content-Type
header automatically. Let’s explore it.
00:27
So, if it hasn’t already been imported, let’s make sure we import requests
and let’s save the URL that we’re going to use a whole bunch here.
00:38
This time, it’s all about posting. Let’s try our data in a couple of different formats here and see what it looks like. Save it into a Response
, and as you can see, there’s a couple of ways you can send the data. We can send it in—in this case—in a dictionary or a tuple, or in the case of JSON, we said it like this. Let’s try our dictionary.
01:02
Okay. We can look at our response
, it was successful. Let’s turn it into a JSON response.
01:13
So, there we can see what our request returned back in our response, and we can see the data going in here. There’s a 'form'
. Let’s talk about that a little bit more. So json_response
, and we’ll look at the header, and what is the 'Content-Type'
in this case?
01:31 So, it comes in this form—as a web form. Let’s see what it looks like when we send it a list of tuples.
01:47
Okay. Close my list. And success. Let’s save this into a json_response
also, which you can see here. We can do the same trick. Let’s look at the 'Content-Type'
this time… and it’s the same. And we can see that 'form'
data here. In fact, let’s look at the 'form'
.
02:09
Maybe if I spell the json_response
correctly… and there’s our data. Great! All right. We’ve got one more thing to try. Let’s send it as JSON this time.
02:33
All right, success! That’s good. And let’s look at this data. So it’s a little different this time, it’s laid out a little differently. Let’s look at the header for the 'Content-Type'
.
02:51 So this time, it converted it into JSON and it serialized it automatically for us and it created that header for us, which is nice, to indicate to the application that it’s coming in as JSON.
techsukenik on Sept. 15, 2021
@Frank The API determines what type of input it accepts (JSON, etc). In other words, the program on the server receiving the request determines what input it accepts.
Patch is used for updates. See: realpython.com/api-integration-in-python/#patch
Become a Member to join the conversation.
Frank on May 22, 2020
I see the differences illustrated between using
data
vsjson
but how do you know when to use which one?I don’t think you mentioned anything about PATCH what is it used for?