1

I am developing an Python REST api and for server side I'm using Django with django-rest-framework. I managed to successfully test my api with AdvancedRestClient in chrome, but I can't get it working with python requests.

My ARC request looks like this: enter image description here

And for my test Python request I wrote the following code:

import requests
import json

payload = {"TimeStamp": "2016-02-07T13:38:16Z", "DateInserted": "2016-02-07T13:38:18Z", "Value": 17.145, "Sensor": 1}
url = "http://127.0.0.1:8000/api/Readings"
headers = {"Content-Type": "application/json", "Authorization": "966271f8-94a3-4232-95f7-953f3b7989f3"}
r = requests.put(url, data=json.dumps(payload), headers=headers)

I tried many different things as using json= instead of data= but my request always seem to have no data content when they arrive to my server side. I searched the web but couldn't find any viable examples of using POST with requests so I'm hoping someone has some first-hand experience in their sleeve to share with me.

Update: The following code now works with django-rest-framework.

payload = {"TimeStamp": "2016-02-07T13:38:16Z", "DateInserted": "2016-02-07T13:38:18Z", "Value": 12.123, "Sensor": 1}
url = "http://127.0.0.1:8000/api/Readings/"
headers = {"Content-Type": "application/json", "Authorization": "966271f8-94a3-4232-95f7-953f3b7989f3"}
r = requests.put(url, data=json.dumps(payload), headers=headers)
4
  • 3
    Your request looks correct to me. Try using http://httpbin.org/put to see what you are actually sending. Commented Feb 7, 2016 at 17:12
  • Can you add Token before your token? " Token '966271f8-94a3-4232-95f7-953f3b7989f3'" Commented Feb 7, 2016 at 17:15
  • Please add print json.dumps(json.loads(r.text), indent=4) and print r.status_code to your snipplet and report the result. (Also add the Token in the Authorization header.) Commented Feb 7, 2016 at 17:37
  • I'm using my own custom Authorization so Token in not needed. I managed to get it working please see comment below. Commented Feb 8, 2016 at 15:59

1 Answer 1

1

DRF has a trailing space by default. Instead of http://127.0.0.1:8000/api/Readings it should probably be http://127.0.0.1:8000/api/Readings/ . I would suggest making Readingsbe lowercase as it's easy to forget that it's capitalized


The general structure for API tests that I've used is this:

from django.core.urlresolvers import reverse

from rest_framework.test import APITestCase, APIClient

class TestReadings(APITestCase):

    def setUp(self):
        self.client = APIClient()

    def test_put(self):

        url = reverse('readings', kwargs={}) # 'readings' here is the named url
        request = self.client.post(url, data={})
Sign up to request clarification or add additional context in comments.

1 Comment

I managed to get it working. In order to do so I had to change two things. When I added / to Readings I started to see data coming through but the format was not correct. So the second thing I had to change was to put data= instead of json=. Thank you all for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.