Forums

POST request is not JSON.. but it works locally.

Hi. I have this POST route:

def post_session_data():
    if request.is_json:
        request_data = request.get_json()
        print("Received JSON data:", request_data)
    else:
        request_data = None
        print("Request is not JSON")

request_data = request.get_json()

print (request_data)

if not request_data:
    print ("boss_battles/post_session data, no data received")
    return "request_data null, no data received"

if request.method == 'GET':
    print ("boss_battles/post_session data no data received")
    return "GET method... not supported?"

if request_data['testingMode'] == True:
    print ("boss_battles/post_session data testing mode, not saving data")
    return "testing mode, not saving data"

When I run it locally... postman local https://imgur.com/tJZc1Hi

When I try and post to the server... postman server https://imgur.com/PmT0RpD server log https://imgur.com/8QhsMdp

Not sure how to fix this.. I've also checked previous forum posts but it seems no one has this issue, thanks.

Check what request you are getting instead of json.

2024-10-07 11:32:33 Request is not JSON <Request 'https://www.sdgames.co/boss_battles/post_session_data' [GET]>

def post_session_data():

    if request.is_json:
        request_data = request.get_json()
        print("Received JSON data:", request_data)
    else:
        # request
        print("Request is not JSON", request)

    request_data = request.get_json()

    print (request_data)

hmm.... I guess cause I have the GET there?

[formatted by admin]

You could add a json to a GET request as a stringified param in the url, if that is the problem you're trying to solve.

Hi sorry, no I'm trying to get the post request to work on the server, it works locally.

this is my new code to test

def post_session_data():

if request.method == 'GET':
    return "This endpoint only accepts POST requests with JSON data."

if request.method == 'POST':
    if request.is_json:
        request_data = request.get_json()
        print("Received JSON data:", request_data)
    else:
        print("Request is not JSON", request.get_data)
        return "Request is not JSON"

request_data = request.get_json()

print (request_data)

https://imgur.com/bK6njCY

it seems to force a GET request? even though its post?

Check the content that is being returned so you can see what it is instead of the JSON that you expect.

Sorry, I think we aren't on the same page, I'm posting image of my code because the code block is ignoring my route code. https://imgur.com/sSUwwuY https://imgur.com/FhCSZtl

I'm sending a POST but the server thinks its a GET...

Then there's probably a redirect happening. It could be an upgrade from http to https or it could be that you defined the endpoint so that it requires a trailing slash, but you're making the request without the trailing slash

I sent the POST to https://sdgames.co/boss_battles/post_session_data/ and https://sdgames.co/boss_battles/post_session_data (HTTPs and trailing /) now getting a timeout error

POST https://sdgames.co/boss_battles/post_session_data

Error: connect ETIMEDOUT 174.129.25.170:443 Request Headers Content-Type: application/json User-Agent: PostmanRuntime/7.42.0 Accept: / Postman-Token: f23123123 Host: sdgames.co Accept-Encoding: gzip, deflate, br Connection: keep-alive

and with the trailing / i get the html code returned for my redirect

It's looks like it's about your redirect. It's not something happening on PythonAnywhere. What if you try www.sdgames.co that is actual domain of your web app?

still doesnt work :(

POST sdgames.co/boss_battles/post_session_data

it stills says its' a get request ...

changed code to this and this is the result.. https://imgur.com/DajaiUj

boss_battles.route(/post_session_data, methods=["POST"]) def post_session_data():

if request.method == 'GET':
    return "This endpoint only accepts POST requests with JSON data."

if request.method == 'POST':
    if request.is_json:
        request_data = request.get_json()
        print("Received JSON data:", request_data)
    else:
        print("Request is not JSON", request.get_data)
        return "Request is not JSON"

again still works locally hosted...

needed to add www ... https://www.sdgames.co/boss_battles/post_session_data

Does it work now?