close

How to convert request.data to dict?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to convert request.data to dict in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to convert request.data to dict?

  1. How to convert request.data to dict?

    you need to deserialize response.text to have it as dict and then user .get with respective key. result is a key in above example. and response was the response of a url call.

  2. convert request.data to dict

    you need to deserialize response.text to have it as dict and then user .get with respective key. result is a key in above example. and response was the response of a url call.

Method 1

Edit: For post request:

import requests
import json
url = "https://jsonplaceholder.typicode.com/posts/"

payload = {
    "userId": 10,
    "id": 901,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
 }
headers = {
    'content-type': "application/json",
    'cache-control': "no-cache",
    'postman-token': "c71c65a6-07f4-a2a4-a6f8-dca3fd706a7a"
    }

response = requests.request("POST", url, data=json.dumps(payload), headers=headers)

print(type(response.json()))

class ‘dict’


You can use something like this:

import requests

url = "https://api.icndb.com/jokes/random"

headers = {
    'cache-control': "no-cache",
    'postman-token': "77047c8b-caed-2b2c-ab33-dbddf52a7a9f"
    }

response = requests.request("GET", url, headers=headers)

print(type(response.json()))

class ‘dict’

Method 2

import json

response_data = json.loads(response.text)
result = response_data.get('result')

you need to deserialize response.text to have it as dict and then user .get with respective key. result is a key in above example. and response was the response of a url call.

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read