Converting cURL to Python Requests - python

I need to convert the following cURL command to python requests.
curl 'https://test.com/api/v1/courses/xx/discussion_topics/xx/entries.json' \
-F 'message=<message>' \
-H "Authorization: Bearer <token>"
So far I have requests.post("https://test.com/api/v1/courses/xx/discussion_topics/xx/entries")
but how do I add the message and auth?

requests.post("https://test.com/api/v1/courses/xx/discussion_topics/xx/entries.json, data=json.dumps({"message": "<message>"}), headers={"Authorization": "Bearer <token>"})

Related

API call to Fathom Analytics via Postman

I'm trying to make an API call to Fathom Analytics. But I cannot figure out the filtering option (e.g. filter reportings for specific pathname). Can someone help me figuring this out?
curl --location --request GET 'https://api.usefathom.com/v1/aggregations?entity=pageview&entity_id=[SITE_ID_HERE]&aggregates=visits, uniques, pageviews, avg_duration, bounce_rate' \
--header 'Authorization: Bearer [BEARER_TOKEN_HERE]'
How can I insert the json-payload here in order to use the Fathom Filter option (https://usefathom.com/api#aggregation)
Also, I rebuilt the api call in python, without success. When I remove the payload from the API call it works just fine.
import requests
endpoint = "https://api.usefathom.com/v1/aggregations?entity=pageview&entity_id=[SITE_ID]&aggregates=pageviews,visits,uniques,avg_duration,bounce_rate&date_from=2022-10-01&date_to=2022-10-31"
payload = [{"property": "pathname",
"operator": "is",
"value": "/[URL TO FILTER]"}]
headers = {
"Authorization": "[BEARER_TOKEN]"}
response=requests.get(endpoint, json=payload, headers=headers)
print(response.json())
I struggled a bit with this too, and in my case it worked when I included the "filter" key name in the JSON payload itself rather than the curl command.
Here's what worked for me:
curl https://api.usefathom.com/v1/aggregations \
-H "Authorization: Bearer [BEARER_TOKEN_HERE]" \
-H "Accept: application/json" \
-d entity="pageview" \
-d entity_id="[SITE_ID_HERE]" \
-d aggregates="pageviews" \
-d "{'filters': [{'property':'country', 'operator':'is', 'value':'UK'}]}" \
-G
Moving the JSON to its own file (payload.json) and updating that curl line to this also worked:
-d #payload.json \

Authorization missing error when using the token as header using python requests module

Trying to convert curl below to the Python requests, below is the curl command which is working fine.
curl -k -H "Authorization: Token token=\"$token\""https://conjur.com/secret
Above curl works fine and gives expected output but when I turn that into python requests it is giving me trouble the header is weird not sure how to pass that, i tried below
token="abcdef"
token_header = {'Authorization': f'Token token={token}'}
requests.get("https://conjur.com/secret", headers=token_header).text
Above code gives error and looks like header is not working as expected,let me know how can i solve it ?
Already mentioned above
In your curl command you have quotes around the token:
curl -k -H "Authorization: Token token=\"$token\"" https://conjur.com/secret
Note the token=\"$token\"
To have quotes in your Python code do:
token_header = {"Authorization": f'Token token="{token}"'}

how to curl file using python

I have curl code recived a image file
curl -X GET -H 'Authorization: Bearer TOKENS' https://api.line.me/v2/bot/message/8595925133330/content -o image.png
how to get the file using python?
curl -X GET -H 'Authorization: Bearer TOKENS' https://api.line.me/v2/bot/message/8595925133330/content -o image.png
Comparable Python Script should be like below:
import requests
endpoint = "https://api.line.me/v2/bot/message/8595925133330/content"
headers = {"Authorization":"Bearer TOKENS"}
r = requests.post(endpoint,headers=headers)
open('image.png', 'wb').write(r.content)

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.

Categories

Resources