Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

PATCH and PUT don't work as expected when pytest is interacting with REST framework

$
0
0

I am building an API using the django REST framework.

To test this API I am using pytest and the test client like so:

def test_doesnt_find(self, client):    resp = client.post(self.url, data={'name': '123'})    assert resp.status_code == 404

or

def test_doesnt_find(self, client):    resp = client.get(self.url, data={'name': '123'})    assert resp.status_code == 404

both work when using the general GET, POST and DELETE Classes of the REST framework (like DestroyAPIView, RetrieveUpdateAPIView or just APIView using get and post functions)

Where I get problems is when using PATCH and PUT views. Such as RetrieveUpdateAPIView. Here I suddenly have to use:

resp = client.patch(self.url, data="name=123", content_type='application/x-www-form-urlencoded')

or

resp = client.patch(self.url, data=json.dumps({'name': '123'}), content_type='application/json')

If I simply try to use the test client like I am used to, I get errors:

rest_framework.exceptions.UnsupportedMediaType: Unsupported media type "application/octet-stream" in request.

And when I specify 'application/json' in the client.patch() call:

rest_framework.exceptions.ParseError: JSON parse error - Expecting property name enclosed in double quotes: line 1 column 2 (char 1)`

Can anyone explain this behavior to me? It is especially hard to catch as curl simply works as well using -X PATCH -d"name=123".


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>