简体   繁体   中英

Django service on gunicorn POST request is recieved as GET?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM