无法使用python请求将POST请求发送到Heroku服务器

问题描述 投票:0回答:1

因此,我正在尝试将带有外部程序的发帖请求发送到我的网站。它在localhost上运行良好,但是现在当我的服务器由Heroku托管时,它却没有。我正在使用requests module

req_id = requests.post('http://admin-monitor.herokuapp.com/computers/verify_login',
                                   json={"MAC address: ": computer_mac_address()})

当然,我在服务器中有此URL的句柄。当我查看日志时,无缘无故地看到500 error

"POST /computers/verify_login HTTP/1.1" 500 290 "-" "python-requests/2.23.0"

我怀疑会不会对您有所帮助,以防万一这是请求的处理程序

app.route('/computers/verify_login', methods=['POST', 'GET'])
def check_if_user_exists():
    if request.method == 'POST':
        global new_mac_address
        js = request.get_json()
        print(js)
        if js is not None:
            for mac_address in js.values():
                for i in range(len(Todo.query.all())):
                    if mac_address == Todo.query.filter(Todo.id).all()[i].mac_address:
                        print("True!")
                        return str(Todo.query.filter(Todo.id).all()[i].id)
                new_mac_address = mac_address
                print("Not found!")
                return redirect('/computers/add')
        else:
            print("Empty? wtf")
    else:   
        return ""
python flask heroku python-requests
1个回答
0
投票

您发送到服务器的json无效。我将POST请求更改为:

req_id = requests.post('http://admin-monitor.herokuapp.com/computers/verify_login',
                               json={"mac_address": computer_mac_address()})

然后在服务器上,您必须从json中提取mac地址。因此,我将替换为:

for mac_address in js.values():

with

mac_address = js['mac_address']
© www.soinside.com 2019 - 2024. All rights reserved.