null
null

Reputation: 171

Acessing POST field data without a form (REST api) using Django

In the django documentation, it says:

HttpRequest.POST

A dictionary-like object containing all given HTTP POST parameters, providing that the request contains form data. See the QueryDict documentation below. If you need to access raw or non-form data posted in the request, access this through the HttpRequest.body attribute instead.

However, the server does not respond to a browser (such as using JS frameworks or a form) but instead a REST api sent by an Anroid/iOS application.

If the client sends fields directly in a POST request, how can I read the data? For example, this (Java + Unirest):

Unirest.post("/path/to/server")
       .field("field1", "value2")
       .field("field2", "value2");

EDIT: Can I simply read the data usingresponse.POST["field1"], or will I have to do something with request.body?

EDIT 2: So I can simply use request.body as a dictionary-like object similar to request.POST?

Upvotes: 17

Views: 3051

Answers (3)

Dhia
Dhia

Reputation: 10619

If the api you are interacting with is a sipmle Django Class Based view, you access the data through request.body something like this:

class MyView(View):
    def post(self, request):
        field1 = request.body.get('field1')
        field2 = request.body.get('field2')
        ... # processing here

In case you are using Django rest framework api, you access the data through request.data:

field1 = request.data.get('field1')
field2 = request.data.get('field2')

NB: If you find request.DATA used somewhere in Internet that's correct too, but it's only valid for old version of DRF, and it's deprecated in favor of request.data in the newer versions.

Upvotes: 6

Nabeel Ahmed
Nabeel Ahmed

Reputation: 19282

From the docs:

request.data returns the parsed content of the request body. This is similar to the standard request.POST and request.FILES attributes except that:

  • It includes all parsed content, including file and non-file inputs.
  • It supports parsing the content of HTTP methods other than POST, meaning that you can access the content of PUT and PATCH
    requests.
  • It supports REST framework's flexible request parsing, rather than just supporting form data. For example you can handle incoming JSON data in the same way that you handle incoming form data.

Can I simply read the data using response.POST["field1"], or will I have to do something with request.body?

So I can simply use request.body as a dictionary-like object similar to request.POST?

An example - From a create method (viewsets):

user = dict(
                full_name=request.DATA['full_name'],
                password=request.DATA['password'],
                email=request.DATA['email'],
                personal_number=request.DATA['personal_number'],
                user_type=request.DATA['user_type'],
                profile_id=request.DATA['profile_id'],
                account_id=request.DATA['account_id']
            )

Edit 1: In version 3 (latest) - request.DATA has been replaced with request.data:

user = dict(
                    full_name=request.data['full_name'],
                    password=request.data['password'],
                    email=request.data['email'],
                    personal_number=request.data['personal_number'],
                    user_type=request.data['user_type'],
                    profile_id=request.data['profile_id'],
                    account_id=request.data['account_id']
                )

Upvotes: 6

NEOatNHNG
NEOatNHNG

Reputation: 934

As far as I understand the field method from Unirest just uses normal application/x-www-form-urlencoded data like a HTML form. So you should be able to just use response.POST["field1"] like you suggested.

Upvotes: 8

Related Questions