16👍
✅
You need to send the payload
as a serialized json
object.
import json
import requests
payload = {"foo":"bar"}
headers = {'Content-type': 'application/json'}
r = requests.put("https://.../myPk/", data=json.dumps(payload), headers=headers)
Otherwise what happens is that DRF will actually complain about:
*** ParseError: JSON parse error - No JSON object could be decoded
You would see that error message by debugging the view (e.g. with pdb or ipdb) or printing the variable like this:
def update(self, request, pk = None):
print pk
print str(request.data)
5👍
Check 2 issues here:-
- Json format is proper or not.
- Url is correct or not(I was missing trailing backslash in my url because of which I was facing the issue)
Hope it helps
- "TemplateSyntaxError Invalid block tag: 'trans'" error in Django Templates
- Django database synchronization for an offline usage
- How to import my django app's models from command-line?
- How to mock chained function calls in python?
1👍
Assuming you’re on a new enough version of requests you need to do:
import requests
payload = {"foo":"bar"}
r = requests.put("https://.../myPk", json=payload, headers=headers)
Then it will properly format the payload for you and provide the appropriate headers. Otherwise, you’re sending application/x-www-urlformencoded
data which DRF will not parse correctly since you tell it that you’re sending JSON.
- Performance, load and stress testing in Django
- Maintain SQL operator precedence when constructing Q objects in Django
- How do I call a model method in django ModelAdmin fieldsets?
- Django – Import views from separate apps
- Django template variable value to string literal comparison fails
Source:stackexchange.com