【问题标题】:Sending a POST request to my RESTful API(Python-Flask), but receiving a GET request向我的 RESTful API(Python-Flask)发送 POST 请求,但收到 GET 请求
【发布时间】:2018-07-05 22:30:24
【问题描述】:

我正在尝试以包含 JSON 的 POST 请求的形式向 Zapier webhook 发送触发器。如果我只是通过本地 python 脚本发送 POST 请求,它就可以正常工作。

我想要做的是创建一个 RESTful API,它在调用 create-row-in-gs 端点时触发 Zapier webhook。

如您所见,我正在向 Hasura 集群发送一个 POST 请求 API 调用。但是,我得到的响应不是“200 OK SUCCESS”,而是“200 OK failure”,这意味着该请求被视为 GET 请求而不是 POST 请求。

test.py

#Python 3 Script to send a POST request containing JSON

import json
import requests

api_url = 'http://app.catercorner16.hasura-app.io/create-row-in-gs'
create_row_data = {'id': '1235','name':'Joel','created-on':'27/01/2018','modified-on':'27/01/2018','desc':'This is Joel!!'}
r = requests.post(url=api_url, data=create_row_data)
print(r.status_code, r.reason, r.text)

server.py(在 Hasura 集群上运行)

from src import app
from flask import jsonify,request,make_response,url_for,redirect
from json import dumps
from requests import post

url = 'https://hooks.zapier.com/hooks/catch/xxxxx/yyyyy/'

@app.route('/create-row-in-gs', methods=['GET','POST'])
def create_row_in_gs():
    if request.method == 'GET':
        return make_response('failure')
    if request.method == 'POST':
        t_id = request.json['id']
        t_name = request.json['name']
        created_on = request.json['created_on']
        modified_on = request.json['modified_on']
        desc = request.json['desc']

        create_row_data = {'id': str(t_id),'name':str(t_name),'created-on':str(created_on),'modified-on':str(modified_on),'desc':str(desc)}

        response = requests.post(
            url, data=json.dumps(create_row_data),
            headers={'Content-Type': 'application/json'}
        )
        return response

已经为此苦苦挣扎了好几个星期。我究竟做错了什么?将不胜感激。

【问题讨论】:

  • 与您的问题无关,但如果您的请求失败,则不应返回 200。
  • @DanielRoseman 好的!会记下的。
  • 我的猜测:你得到了一个重定向。试试requests.postallow_redirects=False
  • @VPfB 是的,我现在这样做了,现在它显示为“301 永久移动”。预期?
  • @JoelKingsley 正如我所说,我只是从症状中猜测,可能会通过隐藏在某处的 post/redirect/get 模式。

标签: python flask python-requests zapier hasura


【解决方案1】:

好的,我在本地检查了您的脚本,发现了两个问题。两者都在您的客户端脚本中。

1) r = requests.post(url=api_url, data=create_row_data) 应该是 r = requests.post(url=api_url, json=create_row_data)

2) 您在 Flask 应用中查找 created_onmodified_on,但您发送的是 created-onmodified-on

下面的工作本地代码:

客户:

import json
import requests

api_url = 'http://localhost:5000/create-row-in-gs'
create_row_data = {'id': '1235','name':'Joel','created_on':'27/01/2018','modified_on':'27/01/2018','desc':'This is Joel!!'}
print(create_row_data)
r = requests.post(url=api_url, json=create_row_data)
print(r.status_code, r.reason, r.text)

服务器:

from flask import Flask,jsonify,request,make_response,url_for,redirect
import requests, json

app = Flask(__name__)

url = 'https://hooks.zapier.com/hooks/catch/xxxxx/yyyyy/'

@app.route('/create-row-in-gs', methods=['GET','POST'])
def create_row_in_gs():
    if request.method == 'GET':
        return make_response('failure')
    if request.method == 'POST':
        t_id = request.json['id']
        t_name = request.json['name']
        created_on = request.json['created_on']
        modified_on = request.json['modified_on']
        desc = request.json['desc']

        create_row_data = {'id': str(t_id),'name':str(t_name),'created-on':str(created_on),'modified-on':str(modified_on),'desc':str(desc)}

        response = requests.post(
            url, data=json.dumps(create_row_data),
            headers={'Content-Type': 'application/json'}
        )
        return response.content

if __name__ == '__main__':
    app.run(host='localhost',debug=False, use_reloader=True)

【讨论】:

  • 忘了说,但是一些导入也抛出了错误
  • 发现这对摆脱这两个问题很有用。但主要问题仍然存在。可能是特定于集群服务的问题,因为它在本地也适用于我。
【解决方案2】:

确保您使用的是正确的协议。 httphttps

如果您使用 http 并看到重定向,则重定向 Location 标头通常会有正确的 URL。

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 2015-07-25
    • 2021-09-19
    • 2015-01-26
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 2021-02-06
    • 2020-08-28
    相关资源
    最近更新 更多