1

I just want to try simple Django rest framework code, I used APIVIEW and posted data through browser.

CODE:

class GetData(APIView):

    def post(self, request, format=None):
        if request.method == 'POST':
            item_id = request.data.get('item_id')
            return Response({"return": "OK ","item_id":item_id})
        else:
            return Response({"return": "NOT OK "})

The above code should echo the item_id whatever i "POST"ed. but the result is always null.

POST data:

{ "item_id" : "3456787"}

result:

{"item_id":null,"return" :"OK "}

I don't understand where I went wrong. the above code works fine in my Loaclhost but does not work in production server.

19
  • What is the data you are posting? and what are the headers? Commented Mar 10, 2017 at 7:18
  • I am using browsable api. Using chrome browesr to post data. mediaType is application/json. Commented Mar 10, 2017 at 7:25
  • Content-Type: application/json Commented Mar 10, 2017 at 7:27
  • Isn't the data in request.body? if it's a dict: request.body.get('item_id') ? Commented Mar 10, 2017 at 7:28
  • @DA-- No, He is using DRF which parses request.body and puts it in request.data Commented Mar 10, 2017 at 7:31

1 Answer 1

1

Started with a fresh project with latest django and DRF and changed the following

app1/views.py

class GetData(APIView):
def post(self, request, format=None):
    if request.method == 'POST':
        item_id = request.data.get('item_id')
        return Response({"return": "OK ","item_id": item_id})
    else:
        return Response({"return": "NOT OK "})

only change in proj/settings.py

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework'
]

app/urls.py

from django.conf.urls import url
from django.contrib import admin
from app1.views import GetData
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^test/', GetData.as_view())
]

Query:

curl -X Post http://localhost:8000/test/ -H "Content-Type: application/json" -d '{ "item_id":"3456787"}'

Result:

{"item_id":"3456787","return":"OK "}%
Sign up to request clarification or add additional context in comments.

2 Comments

As I mentioned earlier in the question. It is working fine in the localhost but not in the production server. I found similar problem here github.com/tomchristie/django-rest-framework/issues/3623 . It was a problem of apache configuration. but there was no correct solution there in the link
@Naroju, I have the same problem as it works properly in local and the same your problem in iis server.

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.