pythonjsondjangolabview

Django receiving json post request from external source


I have written a view function that processes a post request containing json data from a source outside of django (labview). I'm just testing it to begin with so it looks like this

def post_entry(request):
    '''Process incoming json string
    '''

    if request.method == 'POST':

        post_data = request.body

    # Return a response
    return HttpResponse('data received OK')

I've written a test to test this and it passes fine:

def test_post_entry_view_good_post_data(self):
    '''post_entry view should return a 200 status if valid
    '''

    data = {'DHTP Data': ['10', '50.296', '50.94', '50.418', '50.425', '50.431', '50.94'],
        'Test String': 'My Test String'}

    request_url = reverse('post_entry') 
    response = self.client.post(request_url, content_type='application/json', 
        data=dumps(data))

    # Should return a 200 response indicating ok
    self.assertEqual(response.status_code, 200)

But when labview posts the data post_entry returns a 403 forbidden error. I guess this is due to no csrf token being present, but why does the test pass in this case?


Solution

  • The test client works around the CSRF functionality. See https://docs.djangoproject.com/en/1.9/ref/csrf/#testing