Sending a POST request to my RESTful API(Python-Flask), but receiving a GET request

2024/10/5 5:11:58

I'm trying to send a trigger to a Zapier webhook in the form of a POST request containing JSON. It works fine if I just send the POST request through a local python script.

What I want to do is create a RESTful API which makes the trigger to the Zapier webhook when the create-row-in-gs endpoint is called.

As you can see, I'm sending a POST request API call to the Hasura cluster. But instead of getting the response as '200 OK SUCCESS', I'm getting a '200 OK failure' which means that the request is being treated as a GET request instead of a POST request.

test.py

#Python 3 Script to send a POST request containing JSONimport json
import requestsapi_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 (Running on Hasura cluster)

from src import app
from flask import jsonify,request,make_response,url_for,redirect
from json import dumps
from requests import posturl = '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

Have been struggling with this for weeks. What am I doing wrong? Would appreciate any help.

Answer

Ok, I checked you script locally and found two issues. Both are in your client script.

1) r = requests.post(url=api_url, data=create_row_data) should be r = requests.post(url=api_url, json=create_row_data)

2) You look for created_on and modified_on in your Flask app, but you send created-on and modified-on.

Working local code below:

Client:

import json
import requestsapi_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)

Server:

from flask import Flask,jsonify,request,make_response,url_for,redirect
import requests, jsonapp = 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.contentif __name__ == '__main__':app.run(host='localhost',debug=False, use_reloader=True)
https://en.xdnf.cn/q/70517.html

Related Q&A

django deploy to Heroku : Server Error(500)

I am trying to deploy my app to heroku. Deploy was done correctly but I got Server Error(500). When I turned DEBUG true, Sever Error doesnt occurred. So I think there is something wrong with loading st…

Should I preallocate a numpy array?

I have a class and its method. The method repeats many times during execution. This method uses a numpy array as a temporary buffer. I dont need to store values inside the buffer between method calls. …

dup, dup2, tmpfile and stdout in python

This is a follow up question from here.Where I want do go I would like to be able to temporarily redirect the stdout into a temp file, while python still is able to print to stdout. This would involve …

How do I write a Class-Based Django Validator?

Im using Django 1.8.The documentation on writing validators has an example of a function-based validator. It also says the following on using a class:You can also use a class with a __call__() method f…

python synthesize midi with fluidsynth

I cant import fluidsynth. [Maybe theres an better module?]Im trying to synthesize midi from python or pygame. I can send midi events from pygame. Im using mingus, and it seemed pyfluidsynth would be g…

Convenient way to handle deeply nested dictionary in Python

I have a deeply nested dictionary in python thats taking up a lot of room. Is there a way to abbreviate something like this master_dictionary[sub_categories][sub_cat_name][attributes][attribute_name][s…

Running tests against existing database using pytest-django

Does anybody know how to run Django Tests using pytest-django against an existing (e.g. production) database? I know that in general, this is not what unit tests are supposed to do, but in my case, I…

Python, Timeout of Try, Except Statement after X number of seconds?

Ive been searching on this but cant seem to find an exact answer (most get into more complicated things like multithreading, etc), I just want to do something like a Try, Except statement where if the …

MemoryError while pickling data in python

I am trying to dump a dictionary into pickle format, using dump command provided in python. The file size of the dictionary is around 150 mb, but an exception occurs when only 115 mb of the file is dum…

numpy dimensions

Im a newbie to Numpy and trying to understand the basic question of what is dimension,I tried the following commands and trying to understand why the ndim for last 2 arrays are same?>>> a= ar…