There are a few different formats of the body we can use while sending post requests. In this article, we tried to send post requests to different endpoints of our hello world Django application from the postman. Let's see what is the difference between discussed body formats.
The code used in this article is available on GitHub. Clone the repository, checkout the branch post_request_examples and set up the project as explained in READ.ME
file.
In each section below, first, we have a name or type of body format used in post request via postman, then we have a postman screenshot. After that, we have an equivalent curl code which can be useful for sending requests from the terminal.
After that, the code in view.py
file and then the output of the print statements used in views.
This is just like submitting an HTML form.
When sending a post request from the postman, we select the "form-data" body type. Put the parameter name and values in KEY and VALUE columns.
Equivalent CURL request code:
curl --location --request POST 'localhost:8000/post-example/form-data/' \ --form 'name=pythoncircle' \ --form 'url=https://www.pythoncircle.com'
Accessing request in view.py file:
@csrf_exempt
def post_example_form_data(request):
print('POST', request.POST)
return render(request, 'hw/home.html', {})
The output of print statements:
POST <QueryDict: {'url': ['https://www.pythoncircle.com'], 'name': ['pythoncircle']}>
request.POST
and access the variables.post_data = request.POST.copy() name = post_data.get("name")
curl --location --request POST 'localhost:8000/post-example/raw/' \ --header 'Content-Type: application/json' \ --data-raw '{ "name": "pythoncircle", "url": "https://www.pythoncircle.com" }'
@csrf_exempt
def post_example_raw(request):
print('POST', request.POST)
print('BODY', request.body)
print('JSON', json.loads(request.body.decode('utf-8')))
return render(request, 'hw/home.html', {})
POST <QueryDict: {}> BODY b'{\n\t"name": "pythoncircle",\n\t"url": "https://www.pythoncircle.com"\n}' JSON {'url': 'https://www.pythoncircle.com', 'name': 'pythoncircle'}
request.body
attribute as bytestring
instead of request.POST
. bytestring
into a JSON object or dictionary and then access the variables just like we access variables from any dictionary.curl --location --request POST 'localhost:8000/post-example/form-urlencoded/' \ --header 'Content-Type: application/x-www-form-urlencoded' \ --data-urlencode 'name=pythoncircle' \ --data-urlencode 'name_spaced=python circle' \ --data-urlencode 'url=https://www.pythoncircle.com'
@csrf_exempt
def post_example_form_urlencoded(request):
print('POST', request.POST)
print('BODY', request.body)
print(parse_qs(request.body.decode('utf-8')))
return render(request, 'hw/home.html', {})
POST <QueryDict: {'name': ['pythoncircle'], 'name_spaced': ['python circle'], 'url': ['https://www.pythoncircle.com']}> BODY b'name=pythoncircle&name_spaced=python%20circle&url=https%3A%2F%2Fwww.pythoncircle.com' {'name': ['pythoncircle'], 'name_spaced': ['python circle'], 'url': ['https://www.pythoncircle.com']}
request.POST
as well as request.body
attribute. Data in request.POST
attribute is a QueryDict while in request.body
is urlencoded bytestring.urllib.parse.parse_qs
module.helloworld next line this is third line
curl --location --request POST 'localhost:8000/post-example/binary/' \ --header 'Content-Type: text/plain' \ --data-binary '@/home/rana/helloworld.txt'
@csrf_exempt
def post_example_binary(request):
print('POST', request.POST)
print('BODY', request.body)
return render(request, 'hw/home.html', {})
POST <QueryDict: {}> BODY b'helloworld\nnext line\nthis is third line\n'
request.body
attribute in bytestring format with newline characters. Decode the bytestring and then split it using a newline character and access the one line at a time from array/list of strings.curl --location --request POST 'localhost:8000/post-example/file-upload/' \ --form 'file=@/home/rana/helloworld.txt' \ --form 'name=pythoncircle'
@csrf_exempt
def post_example_file_upload(request):
print('POST', request.POST)
print('FILES', request.FILES)
return render(request, 'hw/home.html', {})
The output of print statements:
POST <QueryDict: {'name': ['pythoncircle']}> FILES <MultiValueDict: {'file': [<InMemoryUploadedFile: helloworld.txt (text/plain)>]}>
request.FILES
attribute. Any file uploaded is available as InMemoryUploadedFile object in this attribute.In this format, the body is not sent in the post request.
Equivalent CURL request code:
curl --location --request POST 'localhost:8000/post-example/none/'
Accessing request in view.py file:
@csrf_exempt
def post_example_none(request):
print('POST', request.POST)
print('BODY', request.body)
print('FILES', request.FILES)
return render(request, 'hw/home.html', {})
The output of print statements:
POST <QueryDict: {}> BODY b'' FILES <MultiValueDict: {}>
Since the body is not sent with this post request, the value of attributes request.POST, request.body and request.FILES is empty.
The code used in this article is available on GitHub. Clone the repository, checkout the branch post_request_examples and set up the project as explained in READ.ME
file.
Host your Django Application for free on PythonAnyWhere. If you want full control of your application and server, you should consider DigitalOcean. Create an account with this link and get $100 credits.