How to make curl post request from python - python

I am trying to make a post request using curl in python but the below script throws error
import os
first_name1 = "raj"
last_name1 = "kiran"
full_name = "raj kiran"
headline = "astd"
location1 = "USA"
current_company1 = "ss"
curl_req = 'curl -X POST -H "Content-Type: application/json" -d '{"first_name":"{0}","last_name":"{1}","current_company":"{2}","title":"{3}","campus":"","location":"{4}","display_name":"{5}","find_personal_email":"true"}' http://localhost:8090'.format(first_name1,last_name1,current_company1,headline,location1,full_name)
os.popen(curl_req)
Error:
SyntaxError: invalid syntax
How to make above program work?

The problem in your code is the quotes. Change it to:
curl_req = '''curl -X POST -H "Content-Type: application/json" -d '{"first_name":"{0}","last_name":"{1}","current_company":"{2}","title":"{3}","campus":"","location":"{4}","display_name":"{5}","find_personal_email":"true"}' http://localhost:8090'''.format(first_name1,last_name1,current_company1,headline,location1,full_name)
But, as mentioned in the comments, requests will always be a better choice.
Requests syntax:
import requests
post_data = {
# all the data you want to send
}
response = requests.post('http://localhost:8090', data=post_data)
print response.text

One good resource I've used for converting a cURL request to Python requests is curlconverter. You can enter your cURL request and it will format it for Python requests.
As a side note, it can also convert for PHP and Node.js.
Hope that helps!

Related

Flask API failing to decode JSON data. Error: "message": "Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)"

I'm setting up a simple rest api using flask and flask-restful. Right now all I'm trying to do is create a post request with some Json data, and then return it just to see if it works. I always get the same error "message": "Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)"
Below is my code
from flask import Flask, jsonify, request
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class Tester(Resource):
def get(self):
return {'about': 'Hello World'}
def post(self):
data_json = request.get_json(force=True)
return {'you sent': data_json}
api.add_resource(Tester, '/')
if __name__ == '__main__':
app.run(debug=True)
The curl request I am making to test this is below, I've also tried making request using postman
curl -H "Content-Type: application/json" -X POST -d '{'username':"abc",'password':"abc"}' http://localhost:5000
You need to select raw -> JSON (application/json) in Postman like this:
When it comes to your cURL request, answer explains that windows's command line lacks support of strings with single quotes, so use:
curl -i -H "Content-Type: application/json" -X POST -d "{\"username\":\"abc\", \"password\":\"abc\"}" 127.0.0.1:5000
instead:
curl -H "Content-Type: application/json" -X POST -d '{'username':"abc",'password':"abc"}' http://localhost:5000
\ escapes " character.
You also need to have the Content-Length header enabled.
Some additional information in case you are using python script to test your flask api like me - you have to dumps before adding the dictionary to the data field.
import requests
import json
response = requests.post(
'http://localhost:5000',
data = json.dumps({'username':"abc",'password':"abc"}),
headers = {"Content-Type": "application/json"}
)
The curl request (payload) is incorrect. Use double quotes in the payload.
curl -H "Content-Type: application/json" -X POST -d '{"username":"abc","password":"abc"}' http://localhost:5000

Submitting Python file in batch mode in Livy(without Hadoop installed)

i have made a simple python file which want to submit in Livy.Livy is currently running in local mode.Also I have mentioned following property in livy.conf file.
Property name: livy.file.local-dir-whitelist,
value "/usr/local/livy/scripts"
My file is kept in following path "/usr/local/livy/scripts"
import json, pprint, requests, textwrap
host = 'http://localhost:8998'
data = {'kind': 'spark'}
headers = {'Content-Type': 'application/json'}
r = requests.post(host + '/sessions', data=json.dumps(data), headers=headers)
r.json()
I am submitting it using curl as follows:
curl -X POST --data '{"file": "/usr/local/livy/scripts/pi.py"}' -H "Content-Type: application/json" 10.140.178.24:8999/batches
It is giving me following error:
requirement failed: Local path /usr/local/livy/scripts/pi.py cannot be added to user sessions.
My Ubuntu system only have following things:
a)Spark
b)Livy
c)Java
What am I doing wrong here?
For people using incubating mode of livy for first time,kindly check that the template file is renamed with stripping off .template in livy.conf.template.Then make sure that the following configurations are present in it.
livy.spark.master = local
livy.file.local-dir-whitelist = /path/to/script/folder/
Kindly make sure that forward slash is present in end of path
Then write url in following manner for
Python:
curl -v -X POST --data '{"file": "/path/to/script/folder/name-of-python-file.py"}' -H "Content-Type: application/json" localhost:8998/batches
Note:It will not accept relative path,whole absolute path needs to be defined in it.
curl -X POST --data '{"file": "/usr/local/livy/scripts/pi.py"}' -H "Content-Type: application/json" 10.140.178.24:8999/batches
{"id":2,"state":"starting","log":[]}

how to use below curl command in python

Below is the curl command and wanted to use in python by using request. I am beginner to python. Appreciate advice/help.
curl --header 'Content-Type: text/xml;charset=UTF-8' --data-binary #c:/abcd.xml -X POST http://www.dneonline.com/calculator.asmx
You can use Requests to POST data:
import requests
url = 'http://www.dneonline.com/calculator.asmx'
files = {'c': open('/abcd.xml', 'rb')}
r = requests.post(url, files=files)
Requests is now a defacto standard.
Either use requests module or call it from a shell.
So,
from subprocess import call
call("curl --header 'Content-Type: text/xml;charset=UTF-8' --data-binary #c:/abcd.xml -X POST",shell=True)

Convert curl command to http request in python

I'm new on REST API's and I'm unable to make run an http request.
If I try the curl command it works by terminal:
curl \
--request POST \
--header "X-OpenAM-Username: user" \
--header "X-OpenAM-Password: password" \
--header "Content-Type: application/json" \
--data "{}" \
http://openam.sp.com:8095/openamSP/json/authenticate
and the result:
{"tokenId":"AQIC5wM2LY4Sfczw67Mo6Mkzq-srfED3YO8GCSe0Be6wtPs.*AAJTSQACMDEAAlNLABM2NzQ5NjQ4Mjc0MDY0MzEwMDEyAAJTMQAA*","successUrl":"/openamSP/console"}
But now, from my web on Django I want to make a request and I'm unable to make it work, the code that I use it's:
import requests
headers = {'X-OpenAM-Username':'user', 'X-OpenAM-Password':'password', 'Content-Type':'application/json'}
data = {}
r = requests.get('http://openam.sp.com:8095/openamSP/json/authenticate', headers=headers, params=data)
and if I check the answer:
u'{"code":405,"reason":"Method Not Allowed","message":"Method Not Allowed"}'
What I'm doing wrong? I can't see where is my mistake.
Thanks and regards.
You are doing everything correct, except POST method just do this:
r = requests.post('http://openam.sp.com:8095/openamSP/json/authenticate', headers=headers, params=data)
The method URL receives is POST method not GET.

Curl Post Json data not being read in Python Django

Am using curl exe in windows, to communicate with my Django backend.
Following is the command am using.
curl --dump-header - -H "Accept: application/json" -H "Content-Type: application/json" -X POST --data "{\"uid\":12,\"token\":\"asdert\"}" http://localhost:8000/restapi/v1/foo/
Now this give the data in wrong format. i.e. in the view the post is showing this data
print request.POST
{"{\"uid\":12,\"access_token\":\"asdert\"}": [""]}
What is the correct way to post json data ?
Edit:
I have tried several other methods for e.g.
I am trying to communiate with my rest api using http://slumber.in/.
Even here am getting the same result as above.
import slumber
api = slumber.API("http://localhost/restapi/v1/"
api.foo.post({"uid":"100"})
Excerpts from the view
print request.POST
{u'{"uid": "100"}': [u'']}
P.S. - curl --dump-header - -H "Accept: application/json" -H "Content-Type: application/json" -X POST --data "uid=12&token=asdert" http://localhost:8000/restapi/v1/foo/
This works. But this is not Json format.
I tried your command with http://httpbin.org/post and it worked fine.
Now, your problem is that you should access the incoming JSON data from
request.raw_post_data
instead of request.POST.
(Or if you are using Django 1.4+, use request.body instead as request.raw_post_data is being deprecated)
Detailed code should be something like this:
import json
if request.method == "POST":
data = json.loads(request.raw_post_data)
print data

Categories

Resources