anderex92
anderex92

Reputation: 29

Python requests.put cannot update object

I'm trying to request put into my API built using Django. When I'm requesting from IDLE I'm not getting my objects updated, but when I use postman and I make request on the same url with the same JSON it works. I don't get the point because the url and JSON are the same.

from tkinter import *
import requests
import json


url = "http://127.0.0.1:8000/rentals/35/"
custom_json = {"client": "dsa", "bike_id_number": ["483KIS5O4D", "473JDS93DF", "837JFIE945"]}

myobj = json.dumps(custom_json)

print(myobj)
requests.put(url, data = myobj)

Screen from Postman with copied url and JSON from my Python Code: https://ibb.co/rmPRtPj

Upvotes: 0

Views: 1439

Answers (1)

Andruida
Andruida

Reputation: 384

You may have to specify the Content-Type: application/json header from your Python code, or you could pass your Python dict directly to requests via the json keyword param (it sets the header automatically for you this way).

requests.put(url, json = custom_json)

Source (what applies to POST here, also applies to any other method with a body)

Upvotes: 2

Related Questions