i am using the following command in terminal and it's working fine,
now i want to get the same result as i get in the terminal.so how i can do this with python script.
actually i need to get the cookies and the curl command give me all cookies values those i needed ,therefore its up best solution for me,so now i want to use it in python script
CURL cmnd:
curl -i -X PUT https://www.snipes.it
USE requests.get() OR requests.post() TO MAKE A CURL REQUEST
Call requests.get(url) and requests.post(url, data, headers) to make a curl request, or any web request. The url is the url of the specified endpoint, data is the payload to send, and headers should contain any relevant headers for the request.
Related
I'm trying to send an HTTP POST request using the python requests package.
The working curl command looks like the following (captured from chrome dev tools network tab, right clicked on someFile.php, and chose "copy as cURL"). When run in a terminal, it outputs a valid, nonempty, response.
curl 'https://somedomain.com/someFile.php' \
--data-raw $'abc=ZXC%20*%20QWE%20***%20BNM%20((someThing%20%3D%200))%20AND%20anotherThing%20%3E%3D%20\'2020-5-9\'%20IOP%20&someparam=1&myhash=a5d96895cab824fbd9bb85627a8f909d'
I attempted to replicate the POST request in python with:
import requests
url = 'https://somedomain.com/someFile.php'
out = requests.post(url,data=r'abc=ZXC%20*%20QWE%20***%20BNM%20((someThing%20%3D%200))%20AND%20anotherThing%20%3E%3D%20\'2020-5-9\'%20IOP%20&someparam=1&myhash=a5d96895cab824fbd9bb85627a8f909d')
print(out.text)
... but this just prints an empty string.
How do I handle this curl command in python?
Simply setting Content-Type header to application/x-www-form-urlencoded should solve your problem.
headers={}
headers["Content-Type"]="application/x-www-form-urlencoded"
data="....."
output=requests.post(url,headers=headers,data=data)
url="https://somedomain.com/someFile.php"
params = {"abc":"text without quote symbols", "someParam":.... }
res=requests.post(url, params=params)
print(res.text)
When I do curl, I get a response:
root#3d7044bac92f:/home/app/tmp# curl -H "Content-type: application/json" -X GET https://github.com/timeline.json -k
{"message":"Hello there, wayfaring stranger. If you\u2019re reading this then you probably didn\u2019t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.","documentation_url":"https://developer.github.com/v3/activity/events/#list-public-events"}
However, when I do python requests to the same URL I get a status 410.
import requests
headers = {
'Content-type': 'application/json',
}
r = requests.get('https://github.com/timeline.json')
print r.json
root#3d7044bac92f:/home/app/tmp# python rest.py
<bound method Response.json of <Response [410]>>
What gives?
The host is a standard Ubuntu docker image and only installed Curl and some python modules. Python -V is 2.7
Note: I looked at this question but I can't telnet into above server so that solution doesn't apply to me:
Curl works but not Python requests
You've made at least two errors in your program.
1) You haven't specified the data= or headers parameters to the requests.get() call. Try this:
r = requests.get('https://github.com/timeline.json', data=data, headers=headers)
2) .json is a method, not a data attribute of the response object. As a method, it must be called in order to be effective. Try this:
print r.json()
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 am interfacing with an API using requests and requests_oauthlib.
I successfully authenticate and access all the GET methods of the API, but get error 500 with POST methods. For example:
r = oauth.post("https://api.timelyapp.com/1.0/1/clients",
data={"client":{"name":"newclient", "color":"c697c0" }},
allow_redirects=False, headers={"Content-Type": "application/json"})
The issue is that I tested the same exact call with curl and it works correctly, here the curl code:
curl -v -X POST -H "Content-Type: application/json" -H "Authorization: Bearer XXXXXXXXXXXX" --data '{"client": { "name": "newclient", "color":"c697c0" }}' "https://api.timelyapp.com/1.0/1/clients"
how can I dig deeper in requests to compare its call with curl?
UPDATE:
Also, noticed that if I do not specify content type:
r = oauth.post("https://api.timelyapp.com/1.0/1/clients",
data={"client":{"name":"newclient", "color":"c697c0" }},
allow_redirects=True)
I get instead a 302 with redirection to the site homepage, where I fetch the content of the page. In any case the new client is not added.
You might want to try this instead:
data=json.dumps(payload)
From python-requests doc:
There are many times that you want to send data that is not
form-encoded. If you pass in a string instead of a dict, that data
will be posted directly.
I am trying to call rest api by sending json data. The curl command is pretty straight forward but only problem that I am facing is with "--data" parameter.
For curl, the data is sent as follows:
curl -X POST -H <headers> --data 'params={...}' <url>
I am not able to figure out how to send the --data parameter with the name 'params='attached to it using python-requests.
Also while making GET requests, there are a lot of options which I have to send along with the curl requests(-O ,-J, -v, -G,-L).
I wanted to know how to supply these additional parameters using python-requests.
Thanks.
curl is a very rich library that has gone far way after a lot of developments in last decades. Compare to the curl, python's requests library is still a baby. So you can not expect all the functionalities of curl in requests. You'll only get the major functionalities of curl in your requests.
Now come to your question. If you want to send the json data through a variable, then the basic POST operation(content-type application/x-www-form-urlencoded) will do.
payload = {'params': json_string}
r = requests.post("http://url/post", data=payload)
But if you want to POST the data as json object with header content-type as json, then you have to use this
headers = {'content-type': 'application/json'}
r = requests.post(url, data=json_string, headers=headers)
In Curl the param -L means for following the redirection. You can achieve it with allow_redirects=True parameter:
r = requests.post(url, data=json_string, headers=headers, allow_redirects=True)
Help yourself to find your needs from the requests document.