PATCH and PUT don't work as expected when pytest is interacting with REST framework
<p>I am building an API using the django REST framework.</p>

<p>To test this API I am using pytest and the test client like so:</p>

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

<p>or</p>

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

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

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

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

<p>or</p>

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

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

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

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

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

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

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

<p>This is usually sign that you send a string inside a string in json.
For example:</p>

<pre><code>resp = client.patch(self.url, data=json.dumps("name=123"), content_type='application/json')
</code></pre>

<p>will cause this kind of issues.</p>

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

<p>This means that the request has been sent as "application/octet-stream" which is Django's test default.</p>

<p>To ease the pain with dealing with all that, Django REST framework provides a client on its own: <a href="http://www.django-rest-framework.org/api-guide/testing/#apiclient" rel="nofollow">http://www.django-rest-framework.org/api-guide/testing/#apiclient</a></p>

<p>Note that the syntax is slightly different from Django's one and that you won't have to deal with json encoding.</p>

python django-rest-framework put pytest-django