2
votes

I have a Django rest service running on virutal environment on gunicorn server with the following .wsgi file:

import os, sys import site

site.addsitedir('/opt/valuation/env/lib/python2.7/site-packages')


sys.stdout = sys.stderr
os.environ['DJANGO_SETTINGS_MODULE'] = 'valuation.valuationcont.valuation.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

When I do curl POST call the service works perfectly:

curl -H "Content-Type: application/json" -X POST -d '{...}' -u username:password http://localhost:8000/valuation/predict/

But when I do the same request on API gateway using axios, Django service responds my custom GET response ("GET not supported, try POST").

axios({
    method: 'post',
    url:'http://localhost:8000/valuation/predict',
    headers:{
      "Content-Type":"application/json",
      "Authorization":"Basic [BASE64 ENCODING]"
    },
    data:{
      ...
    }
}).then(response=>{
  console.log(response.data)
}).catch(err=>{
  console.log(err.toString())
})

The request is transformed from GET to POST. This only happens with the django/gunicorn service.

Since I am new to django/gunicorn I think there is something wrong with the .wsgi file. But how come the curl call then works?

Any help appreciated, been struggling with this for a week now.

Edit:

Managed to recreate the same problem in my local machine. axios POST requests using its API are translated into GET.

Using the axios.post(...) method I managed to get 403 and 201. All while POSTMAN works fine.

I have a suspicion that since the POST fails axios API has a default fallback to GET which then doesn't fail and service responds normally ("GET not supported" as is should).

New step to debug this would be to ask, how do I recreate POSTMAN POST call as close as possible in javascript since POSTMAN is working and it is obviously axios that is causing the problems.

1
Use curl -v to see if there is a redirect.Klaus D.
$ curl -v curl: no URL specified! curl: try 'curl --help' or 'curl --manual' for more informationKasparTr
Sorry, my bad. From the advanced curl line in your question I have implied that you know how to use it. You actually have to add the argument -v to the curl line in your question at a proper position before the URL.Klaus D.

1 Answers

4
votes

You're not using the same URL. In the curl snippet you request http://localhost:8000/valuation/predict/ but in the second you request http://localhost:8000/valuation/predict - without the final slash.

Django by default redirects URLs that don't end in a slash to one that does, and a redirect is always a GET.