Forums

Unable to POST to my django web app

Hi, I created a web-app and when I ran it in local environment, it ran flawlessly. I hosted it here - https://ezpz4me.pythonanywhere.com/ . I am trying to post some data in this url- https://ezpz4me.pythonanywhere.com/create/ - I have chosen all of the options provided- application/json,application-xwwwform,multipart-form data and tried sending a test data like this-

{'item_name': 'Laptop', 'item_rating': '4/5', 'item_offers': '10% off', 'item_price': '59000', 'item_features': 'i7, 16gb ram'} {"item_name":"laptop"}

still it doesn't let me send the data and throws a serialization error, which is -

{ "item_name": [ "This field is required." ], "item_rating": [ "This field is required." ], "item_features": [ "This field is required." ] }.

I am not getting this error in postman or even in the browser when I send data in the same way.

Can someone provide me any insight on what I am doing wrong?

What is the code that you're using to do the POST?

I'm having a very similar issue and I'm wondering if anyone managed to resolve it.

@ain9cm3035 What response are you getting? Do you see anything related in your web app's error log? Are you sure the view supports POST requests? (We need more details to be able to help you.)

Hi there and thank you for your response. I think the processing code is correct because it works fine locally; I just can't get any POST request to work properly on pythonanywhere. The errors present like serializer errors, but I can't possibly think why they would come up. Here is more information:

Link: https://ain9cm3035.pythonanywhere.com/api/create_host/

Sample Input: (which works fine when I run it locally): { "host_id":0, "host_name":"me" }

Code to process POST request:

@api_view(['POST'])

def create_host(request):

       serializer = HostSerializer(data=request.data) 
       if serializer.is_valid(): 
           serializer.save() 
           return Response(serializer.data, status=status.HTTP_201_CREATED) 
       return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Serializer:

class HostSerializer(serializers.ModelSerializer):

    class Meta:
        model = Host
        fields = ('host_id', 'host_name') # host_id is int

Server Response:

POST /api/create_host/
HTTP 400 Bad Request
Allow: POST, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "host_id": [
        "This field is required."
    ],
    "host_name": [
        "This field is required."
    ]
}

**Error log: **

enter code here2025-01-03 09:14:22,635: Bad Request: /api/create_host/

[edit by admin: formatting]

How exactly does your post request look like?

Hi again. I'm not sure if I'm understanding the question correctly, but my code for saving the information in the database is what I provided above:

@api_view(['POST'])
def create_host(request):
   serializer = HostSerializer(data=request.data) 
   if serializer.is_valid(): 
       serializer.save() 
       return Response(serializer.data, status=status.HTTP_201_CREATED) 
   return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

[formatted by admin]

If you are getting Bad Request response it looks like the validation fails. Do you know how the request to /api/create_host looks like when it returns 400 response?

So I tried to get a printout of the request data and here is what I get from the server log:

2025-01-06 10:41:19 <QueryDict: {'_content_type': ['application/json'], '_content': ['{"host_id":0, "host_name":"some name"}']}>
2025-01-06 10:41:19 Bad Request: /api/create_host/

The json object is correctly constructed... I'm really puzzled by this.

[edit by admin: formatting]

What is the exact client-side code that you are running to make the POST request?

I am not running any code except for the block that begins with @api_view(['POST']) quoted above stored in an api.py file. The endpoint in urls.py is defined as follows: path('create_host/', api.create_host, name="create host")

How are you making the POST request to send the data to your Django site?

As I said above, the POST request is processed through the function defined below the @api_view decorator. Am I perhaps misunderstanding the question? This code seems to work fine locally...

The question is about the other end, the client, not the server. Not about how you are handling the request but what is making it.

I guess this is the part that is being handled by the REST framework, is it not?--Could it be an issue with that?

You appear to be sending the POST request as JSON. Perhaps you have not configured the API endpoint to accept JSON.