向RESTful API(Python Flask)发送POST请求,但接收GET请求

2024-05-23 14:20:17 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我想做的是创建一个RESTful API,当调用gs端点中的create行时,它会触发Zapier webhook。

如您所见,我正在向Hasura集群发送请求后API调用。但我得到的不是“200ok成功”的响应,而是“200ok失败”,这意味着请求被视为GET请求而不是POST请求。

测试.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

已经挣扎了好几个星期了。我做错什么了?希望能得到帮助。


Tags: nameimportidjsonurldataonrequest
2条回答

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

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

2)您在烧瓶应用程序中查找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)

确保使用正确的协议。httphttps

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

相关问题 更多 >