Forums

How to return a Sussess to a POST

Hello, I am trying to return success to a POST from TradingView. If they don't get it, they resend up to 3 times! So, I think I'm returning a 200, but not sure. Please tell me:

@app.route('/TV-BIN', methods=['POST'])
def webhook_TV_BIN():
    message = now + " TV-BIN order received"
    db.session.add(Comment(content=message))
    db.session.commit()
    if request.method == 'POST':
        request_data = request.get_json()
        if request_data:
            qty = 0
            price = 0
            side = 'sell'
            symbol = 'BNBUSDT'

            if 'side' in request_data:
                side = request_data['side']
            if 'qty' in request_data:
                qty = request_data['qty']
            if 'symbol' in request_data:
                symbol = request_data['symbol']
            if 'digits' in request_data:
                digits = request_data['digits']
            if 'price' in request_data:
                price = request_data['price']
                price = round(float(price),digits)

            api_key=BIN_config.API
            api_secret=BIN_config.SECRET
            client = Client(api_key, api_secret)

            try:
                response = type(client.create_order(
                    symbol=symbol,
                    side=side,
                    type='LIMIT',
                    timeInForce='GTC',
                    quantity=qty,
                    price=price))
                if type(response) == str:
                    result = response
                else:
                    result = response.get('status')
                message = now + ' : Binance Order ' + result + '- ' + side + ' ' + str(qty) + ' ' + symbol
            except binance.exceptions.BinanceAPIException as err:
                message = now + " BIN Msg: " + str(err)
            except:
                message = now + " BIN order: " + str(response)

            db.session.add(Comment(content=message))
            db.session.commit()
            return Response(status=200)
        return Response(status=200)
    else:
        return Response(status=200)

You can check your web app's access log to see what response codes are being returned.

Yes, I should have included that. Usually the error logs have been very useful, but here there is no "error". The Access log shows:

52.32.178.7 - - [17/Jul/2022:23:30:04 +0000] "POST /TV-BIN HTTP/1.1" 500 1184 "-" "Go-http-client/1.1" "52.32.178.7" response-time=0.835
52.32.178.7 - - [17/Jul/2022:23:30:09 +0000] "POST /TV-BIN HTTP/1.1" 200 0 "-" "Go-http-client/1.1" "52.32.178.7" response-time=0.385
52.32.178.7 - - [18/Jul/2022:01:30:01 +0000] "POST /TV-BIN HTTP/1.1" 500 1184 "-" "Go-http-client/1.1" "52.32.178.7" response-time=1.368

500 is an error response, right? I want to return a success 200

So, how it it that I'm returning a 500? Thanks

You mentioned that you'd been checking the error logs, and I think that's what you need to do here. Take a look at the most recent error at the bottom of the file -- from the logs that I can see as an admin, I can see that there is a stack trace starting at 2022-07-18 01:30:19,916, which is almost the same time as that second 500 error in the access logs.