Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

306
Vistas
Django POST data in request.body but can not store in variable

I am posting some raw JSON to my backend

{
    "access_token": "hU5C7so4reJOYTzPyhKPs8PWq07tb",
    "id": 3,
    "order_details": [{"meal_id": 1, "quantity": 3}, {"meal_id": 2, "quantity": 2}]
}

However when I try to

print (request.POST.get("access_token")) 

I receive None

But, when I do

print (request.body)

The values seem to there: b'{\n\t"access_token": "hU5C7so4reJOYTzPyhKPs8PWq07tb",\n\t"id": 3,\n\t",\n\t"order_details": [{"meal_id": 1, "quantity": 3}, {"meal_id": 2, "quantity": 2}]\n}'

I am using Postman to post the data.

I want to store the access token into some variable like so:

access_token = request.POST.get("access_token")

with the post data

I am still fairly new to Django, any help would be great.

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Request.POST gets populated when you are sending form data. see django doc, and this answer for details. Since, in your case, you are posting 'plain' json, you will find your payload under request.body. You can decode the body from binary to a dict as follows:

import json

body = json.loads(request.body.decode('utf-8'))
access_token = body.get("access_token")
about 4 years ago · Santiago Trujillo Denunciar

0

Just doing a json.loads should work.

import json

json_data = json.loads(request.body)
about 4 years ago · Santiago Trujillo Denunciar

0

you need to decode the json first:

import json
variable = urlopen(request).read().decode('utf-8')
json_obj = json.loads(variable)

then you can retrive your informations

for i in json_obj:
   token = i['access_token']
   order_details = i['order_details']

Hope this help

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda