Edit: I've tried everything in this question and it doesn't solve the issue. Meaning I tried I tried manually adding FormParser and MultiPartParser to DEFAULT_PARSER_CLASSES in settings, and I've tried changing django.test.TestCase to rest_framework.test.APITestCase. I still get the same error code.
When I send a PATCH request to my Django app running on localhost via the command line like so:
http -a <username>:<password> PATCH http://127.0.0.1:8000/post/1/ text="new text"
It works as expected and I get a 200 OK code back.
When I try to do the same thing in my unit test using the django.test.Client.patch
method like this:
In [1]: from django.test import Client
In [2]: client = Client()
In [3]: client.login(username='<username>', password='<password>')
Out[3]: True
In [4]: client.patch('/post/1/', {'text': 'new text'})
Out[4]: <Response status_code=415, "application/json">
I get a 415 (Unsupported Media) response code. The response.data
is Unsupported media type "application/octet-stream" in request.'
If I try adding the parameter content-type='application/json'
to the patch
method (I shouldn't have to do this because I'm able to send GET
, POST
, and DELETE
requests using the Client
class without providing that parameter) I get a 400 error code. and the response.data
is 'JSON parse error - Expecting property name enclosed in double quotes: line 1 column 2 (char 1)'
As I said, when I use the class's get
, delete
, and post
methods, the behavior is as expected.
Am I using the method properly? Is this a bug?
As far as I know, httpie
sends a request with the content-type application/json
.
So, try this:
import json
data = json.dumps({'text': 'new text'})
client.patch('/post/1/', data, content_type='application/json')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With