Curl works but python requests doesn't - python

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()

Related

how do i use curl command in my python script

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.

compare python requests with curl

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.

How to POST a local file using urllib2 in Python?

I am a complete Python noob and am trying to run cURL equivalents using urllib2. What I want is a Python script that, when run, will do the exact same thing as the following cURL command in Terminal:
curl -k -F docfile=#myLocalFile.csv http://myWebsite.com/extension/extension/extension
I found the following template on a tutorial page:
import urllib
import urllib2
url = "https://uploadWebsiteHere.com"
data = "{From: 'sender#email.com', To: 'recipient#email.com', Subject: 'Postmark test', HtmlBody: 'Hello dear Postmark user.'}"
headers = { "Accept" : "application/json",
"Conthent-Type": "application/json",
"X-Postmark-Server-Token": "abcdef-1234-46cc-b2ab-38e3a208ab2b"}
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()
but I am completely lost on the 'data' and 'headers' vars. The urllib2 documentation (https://docs.python.org/2/library/urllib2.html) defines the 'data' input as "a string specifying additional data to send to the server" and the 'headers' input as "a dictionary". I am totally out of my depth in trying to follow this documentation and do not see why a dictionary is necessary when I could accomplish this same task in terminal by only specifying the file and URL. Thoughts, please?
The data you are posting doesn't appear to be valid JSON. Assuming the server is expecting valid JSON, you should change that.
Your curl invocation does not pass any optional headers, so you shouldn't need to provide much in the request. If you want to verify the exact headers you could add -vi to the curl invocation and directly match them in the Python code. Alternatively, this works for me:
import urllib2
url = "http://localhost:8888/"
data = '{"From": "sender#email.com", "To": "recipient#email.com", "Subject": "Postmark test", "HtmlBody": "Hello dear Postmark user."}'
headers = {
"Content-Type": "application/json"
}
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()
It probably is in your best interest to switch over to using requests, but for something this simple the standard library urllib2 can be made to work.
What I want is a Python script that, when run, will do the exact same thing as the following cURL command in Terminal:
$ curl -k -F docfile=#myLocalFile.csv https://myWebsite.com/extension...
curl -F sends the file using multipart/form-data content type. You could reproduce it easily using requests library:
import requests # $ pip install requests
with open('myLocalFile.csv','rb') as input_file:
r = requests.post('https://myWebsite.com/extension/...',
files={'docfile': input_file}, verify=False)
verify=False is to emulate curl -k.

sending data parameters to rest api using python-requests

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.

Python-requests and Meteor / MongoDB collectionapi update not working

I'm trying to use Python Requests to send data to a Meteor application. I'm using the meteor-collectionapi to expose my collection.
I can use CURL to update my collection, like so:
curl -H "X-Auth-Token: 3243EEREFADfdsafkjghk432hljsfDS3" -X PUT -d "{\"\$set\":{\"level\":\"32\"}}" http://localhost:3000/collectionapi/containers/WjyuFkRdmq78qyzR7`
I'd like to perform the same command in Python using Requests. Here's the code I've put together:
import requests
import json
url = 'http://localhost:3000/collectionapi/containers/WjyuFkRdmq78qyzR7'
headers = {'X-Auth-Token': '3243EEREFADfdsafkjghk432hljsfDS3'}
payload = {'\$set':{'level':'43'}}
r = requests.post(url, data=json.dumps(payload), headers=headers)
When I run this the $set doesn't get passed properly and the POST doesn't work correctly (it creates a new object in the collection instead of updating the existing object). I've tried escaping it a variety of ways but nothing seems to work properly. If I don't escape the $set I get:
payload = {'$set':{'level':'38'}}
{"error":"Error: key $set must not start with '$'"}
In your cURL command you're not doing a POST, you're doing a PUT.
Try changing your requests command to
r = requests.put(url, data=json.dumps(payload), headers=headers)

Categories

Resources