How do I convert
curl -X POST -T 'sample_data.json' -H "Content-Type: application/json" https://sample_url.com
to Python using requests?
Specifically, how do I provide "-T" parameter to the request?
I believe you could do something like this:
import requests
import json
requests.post('https://sample_url.com',
headers = {'Content-type': 'application/json'},
data = json.loads(open('sample_data.json').read())
}
You can check out the requests page for more details.
Or, to show a fully self-contained example without having to load the json from a file, you could do:
import requests
requests.post('https://httpbin.org/post', data = {'key':'value'})
<Response [200]>
Note, from the requests docs:
Using the json parameter in the request will change the Content-Type in the header to application/json.
So, you could just do the following instead:
r = requests.post(url, json=payload)
curl -X POST -T 'sample_data.json' \
-H 'Content-Type: application/json' \
https://sample_url.com/
is effectively equivalent to
curl -X POST \
-H 'Content-Type: application/json' \
-d "$(cat sample_data.json)" \
https://sample_url.com/sample_data.json
so using requests, it would be
with open('sample_data.json') as fh:
response = requests.post(
"https://sample_url.com/sample_data.json",
data=fh.read(),
header={'Content-Type': 'application/json'}
)
There are a couple of ways that you could do it. But the best way is to use the requests library. It's not part of the standard library (yet), but makes HTTP requests super straight-forward.
$ pip install requests
or
$ conda install requests
Then
import json
import requests
url = r"https://sample_url.com"
with open("sample_data.json", "r") as fh:
data = json.load(fh)
requests.post(url=url, data=data)
The best way is to use the Python Requests library - https://2.python-requests.org/en/master/. To be able to post a JSON payload in the message is
import Requests
import json
result = requests.post(URL,json=json.loads(open('sample_data.json').read()))
The requests library natively understands how to send your JSON data
There are some more examples of how to post messages with Form encoded payloads on this part of the page as well - https://2.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests
To be able to do other verbs you just need to change the function for requests -
for a GET, requests.get(url)
for a PUT, requests.put(url,data=DATA) or requests.put(url,json=JSON)
for a DELETE, requests.delete(url)
and so on.
Related
How can I send this curl out using Python?
I did find similar requests, but cannot adapt my code to suite the query
I have also tried using pycurl, following this example but without luck.
$ curl -v -X GET -H "Accept: text/csv" -H "Authorization: Basic YW5kcmVhLmJvdHRpQHdzcC5jb206OWY5N2E5YTY2ZWU1MTMxZjdmNjk4MDcwZTFkODEwMjU0M2I0NTg1ZA==" "https://epc.opendatacommunities.org/api/v1/domestic/search"
Thanks
If you are using the Python Requests package the following code snippet should work:
import requests
headers = {
'Accept': 'text/csv',
'Authorization': 'Basic YW5kcmVhLmJvdHRpQHdzcC5jb206OWY5N2E5YTY2ZWU1MTMxZjdmNjk4MDcwZTFkODEwMjU0M2I0NTg1ZA==',
}
response = requests.get('https://epc.opendatacommunities.org/api/v1/domestic/search', headers=headers)
response.status_code # 200
response.text # "lmk-key,address1,address2,address3,postcode,buildi ..."
(Note: I used the https://curl.trillworks.com/ website to automatically make the conversion)
I'm trying to grab some data from a website using API, but I'm having trouble converting the example curl command to python requests.
example curl command
curl -X POST "some_url" \
-H "accept: application/json" \
-H "Authorization: <accesstoken>" \
-d #- <<BODY
{}
BODY
My python requests that didn't work
headers = {
'Authorization': "Bearer {0}".format(access_token)
}
response = requests.request('GET', "some_url",
headers=headers, allow_redirects=False)
I get error code 400, can anyone help me figure out what was wrong?
The equivalent requests code for your curl should be:
import requests
headers = {
'accept': 'application/json',
'Authorization': '<accesstoken>',
}
data = "{} "
response = requests.post('http://some_url', headers=headers, data=data)
You can use https://curl.trillworks.com/ to convert your actual curl invocation (note that it won't handle heredocs, as in your example).
If you see different behavior between curl and your python code, dump the HTTP requests and compare:
Python requests - print entire http request (raw)?
How can I see the request headers made by curl when sending a request to the server?
I am running below API query using curl I am getting json response.
curl -X POST -H 'Content-Type: application/json' -H 'X-Metabase-Session: sdfeffff-sdfdf-ffff-ffff-fffffffff' https://dash.test.sh/api/card/140/query/json
But when I am trying to run same query using python requests I am getting response
"API endpoint does not exist."
Below is the python code which I am trying
import requests
url = 'https://dash.test.sh/api/card/140/query/json'
header = {
'X-Metabase-Session': 'sdfeffff-sdfdf-ffff-ffff-fffffffff'
}
response = requests.get(url, headers=header)
print (response.text)
I expect actual json content
With curl, you are doing a POST request but in Python you are calling GET method of same API. And possibly, the GET method does not exists for the API.
So just change your Python code
From requests.get to requests.post
curl is using Post and you are using get, the issue is there:
response = requests.post(url, headers=header)
Could you please inform what would be Python requests equivalent of following curl code to upload file to knack? Specifically part after -F option. Thank you
curl -X POST "https://api.knack.com/v1/applications/YOUR-APP-ID/assets/file/upload" \
-H 'content-type: multipart/form-data' \
-H 'x-knack-rest-api-key: YOUR-API-KEY' \
-F "files=#/path/to/your/file.txt"
Use requests.post with files and headers. Your curl code is equivalent to:
url = "https://api.knack.com/v1/applications/YOUR-APP-ID/assets/file/upload"
files = {'files':open('/path/to/your/file.txt', 'rb')}
headers = {'x-knack-rest-api-key': 'YOUR-API-KEY'}
r = requests.post(url, headers=headers, files=files)
When using the files argument, requests creates the necessary headers automatically so you don't need to have "Content-Type" or "Content-Length" in headers.
Here is the curl command:
curl -H "X-API-TOKEN: <API-TOKEN>" 'http://foo.com/foo/bar' --data #
let me explain what goes into data
POST /foo/bar
Input (request JSON body)
Name Type
title string
body string
So, based on this.. I figured:
curl -H "X-API-TOKEN: " 'http://foo.com/foo/bar' --data '{"title":"foobar","body": "This body has both "double" and 'single' quotes"}'
Unfortunately, I am not able to figure that out as well (like curl from cli)
Though I would like to use python to send this request.
How do i do this?
With the standard Python httplib and urllib libraries you can do
import httplib, urllib
headers = {'X-API-TOKEN': 'your_token_here'}
payload = "'title'='value1'&'name'='value2'"
conn = httplib.HTTPConnection("heise.de")
conn.request("POST", "", payload, headers)
response = conn.getresponse()
print response
or if you want to use the nice HTTP library called "Requests".
import requests
headers = {'X-API-TOKEN': 'your_token_here'}
payload = {'title': 'value1', 'name': 'value2'}
r = requests.post("http://foo.com/foo/bar", data=payload, headers=headers)