I have a Flask app that uses Celery to run tasks. To start the environment I start a redis server (to store results), then Celery, then my app (python app.py). To kick off a job, I want to issue a POST request with cURL, and I want to pass in a parameter, which will be stored in the key-value:
curl ... -X POST -d '{"key": "value"}'
How does my Python program receive that parameter? Normally you'd use argv when you're just running python app.py, but that doesn't seem to work.
You would curl to the address with post variables.
app.py would try to parse if the request type is POST AND to parse the post variables.
post request
curl ... -X POST -d '{"type": "sendemail","who":"foo#bar.42"}'
curl ... -X POST -d '{"type": "sendsms","who":"+1297581234"}'
app.js (I don't know flask)
if request.type == 'POST':
if post['type'] == 'sendemail':
celery.createjob('sendemail',post['who'])
elif post['type'] == 'sendsms'
celery.createjob('sendsms',post['who'])
learn more about flask and refer to this SO question
If you want to make a POST request, then you can use requests.post()
While on the receiving end. You can use request object provided by flask.
For example:
On the client end:
from requests import post
import json
payload = {'product_id': 'B014F6U5N6', 'website_name': 'amazonIN', 'url': 'xyz'}
r = post("http://localhost:5000", json.dumps(payload), headers={'Content-Type': 'application/json'})
print r.text
On the receiving end:
from flask import request
product_id = request.json['product_id']
url = request.json['url']
website_name = request.json['website_name']
Related
I have a code in python:
cmd = "curl -d 'protection={protection}&Code={Code}' -X POST https://example.com/web/services/toaf6.php"
os.system(cmd.format(protection=protection, Code=Code))
I want to transform this so it uses pythons request libraries???
This request should be sync, but endpoint will return immediately response and I will store transaction ID so I can ask some other endpoint for job status.
I need to send HTTP request with custom method to a custom server. I've been googling about executing curl command in python, and mostly I've found:
Don't do that!
I need to execute the following curl command:
curl -X MUX -i -H "Connection-Service: API" -H "Service-Address: API" http://172.16.117.40
I've been trying with requests library in python with no luck.
I constructed this solution from various stackoverflow answers:
import httplib, urllib2
httplib.HTTPConnection._http_vsn = 10
httplib.HTTPConnection._http_vsn_str = 'HTTP/1.0'
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request(self.url)
request.add_header('Connection-Service', 'API')
request.add_header('Service-Address', 'API')
request.get_method = lambda: 'MUX'
url = opener.open(request)
url.info().getheader('API')
It works as curl command posted in the question and I don't need to mess with the actual command.
I have an API which is currently on HTTP, I moved the API using SSLify library in python flask.
Now when I send data using curl request
curl -v -k -H "Content-Type: application/json" -X POST \
--data '{"title":"foobar","body": "This body"}' \
-L http://X.Y.Z.W.us-west-2.compute.amazonaws.com/test
It returns an empty string to me by using request.data
If I make the request to begin with https it returns correct value. If there is a redirect how can I send data ?
SSLify issues a 301 or 302 redirect status code depending on your configuration. So you need to pass --post301 or --post302 to curl.
The reason for this can be found in the curl man page:
When curl follows a redirect and the request is not a plain GET (for
example POST or PUT), it will do the following request with a GET
if the HTTP response was 301, 302, or 303. If the response code was
any other 3xx code, curl will re-send the following request using
the same unmodified method.
You can tell curl to not change the non-GET request method to GET
after a 30x response by using the dedicated options for that: --post301, --post302 and -post303.
I created an endpoint in the flask file that looks like this
#app.route("/update", methods=['POST', 'GET'])
def update_func():
results = {
"method": request.method
}
return json.dumps(results)
I tried calling this function using both Postman and python, both are saying Flask is processing it as a get request.
import requests
r = requests.post("http://site.fakeurl.org/update", json={})
print r.json()
Is there a config file I need to change for this process as a POST request?
Is this happening to anyone else?
Did you try using http://www.site.faekurl.org/update ?
Some servers redirect http:// to http://www.-- and as a result, the POST request + it's data gets lost in the process.
I am attempting to do a basic request on my Flask-app with cUrl.
This is my Flask code:
#application.route('/fb_checkin/', methods=['POST'])
def checkin():
qr_code = request.form['qr']
provider_name = request.form['provider']
#Lookup Access Key by qr in database
resp = requests.post("https://<USERNAME>:<PASSWORD>#<DOMAIN>.cloudant.com/socialmattic_users/_find",
'{"selector": {"qr": "' + qr_code + '"}}')
token = resp.json()['fb_access_token']
This is my curl command:
curl -X POST 'localhost:5000/fb_checkin/?qr=default&provider=void'
After issuing the curl command an HTTP 400 error is returned.
Is anyone able to tell me why this is the case?
Thanks for any help.
curl should be and also no quotes to URL
curl --data "qr=default&provider=void" http://localhost:5000/fb_checkin/