Hi I use following curl commands to fetch information , here I get session ID and csrf token and then proceed further to do post login and any request.
1. Get /login/, show headers to get the cookies but don't care about output:
curl -o /dev/null -D - 'https://192.168.2.1/login/' --insecure
2. Post /login/. Note how we set the cookies with '-b', pass the parameters with '-d' and set an HTTP Referer header with '-e'.
curl -o /dev/null -D - -b 'csrftoken=TOKEN;sessionid=ID' \
-d 'csrfmiddlewaretoken=TOKEN' \
-d 'username=USER' \
-d 'password=PASSWORD' \
-e 'https://192.168.2.1/login/' \
'https://192.168.2.1/login/'
3. Get list of projects (/api/project/). Here we do care about the output.
curl -D - -b 'csrftoken=TOKEN;sessionid=ID' \
'https://192.168.2.1/api/project/?format=json&limit=0'
How can I automate the same using python , may be any example usingn request or pycurl
Related
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 \
I am trying to convert curl commands to python requests using python scripts. I could find the uncurl modules and could use it to convert few curl commands successfully. But I am facing issues to make my script generic for all curl commands. Following issues are holding back my work. I am trying to write the utility in python which will take curl commands from the text file and will convert commands to python requests one by one.
Commands types like GET / POST etc are not accepted by uncurl.
Curl command options like -u , -X etc are rejected.
For DELETE requests, how should I use uncurl.
curl -v -k -H "Content-Type: application/json" -H "sync_id: 00000000-
0000-0000-0000-000000000001" -H "sync_token:
NhbSzPhbtFlZ9Gm1nLr5f8e0WLGQitG4o00jb006m5Vcs00XVqzRdHcFtyv4YOzd5S02Z3x1iR5OWQINgLP2Og" -H "instance_id: instance_id" -X GET 'example.com'
curl -v -k -H "Content-Type: application/json" -H "sync_id: 00000000-0000-0000-0000-000000000001" -H "sync_token: NhbSzPhbtFlZ9Gm1nLr5f8e0WLGQitG4o00jb006m5Vcs00XVqzRdHcFtyv4YOzd5S02Z3x1iR5OWQINgLP2Og" -H "instance_id: instance_id" -X POST -d '[{"id":"1", "name":"1", "env_mapping_name":"a", "env_mapping_id":"a1"}]' example.com'
The python code which I used for convertsion.
import uncurl
command = 'curl -v -k -H "Content-Type: application/json" -H "sync_id: 00000000-0000-0000-0000-000000000001" -H "sync_token: NhbSzPhbtFlZ9Gm1nLr5f8e0WLGQitG4o00jb006m5Vcs00XVqzRdHcFtyv4YOzd5S02Z3x1iR5OWQINgLP2Og" -H "instance_id: instance_id" -X GET 'example.com'
print uncurl.parse(command)
I have stripped off the tags and curl options which were giving me exceptions as follows.
def stripTags(command):
'''Strip off the unwanted tags from the curl command'''
print '\nCommand is *********************************', command
command = command.replace('-X', '')
command = command.replace('-k', '')
command = command.replace('-v', '')
command = command.replace(' \'', ' \"')
command = command.replace('\' ', '\" ')
command = command.replace('\'', '\"')
command = command.replace(' GET ', '')
command = command.replace(' POST ', '')
command = command.replace(' PUT ', '')
command = command.replace(' POST ', '')
command = command.replace(' DELETE ', '')
print '\nStripped string is =============', command
return command
When I used the following curl command for conversion, I got an exception.
'curl -v --proxy-user "00dcf6e7-4513-4e46-bbaf-ef4cac8f8d47":"XUmh8pIG68Zo" -X GET "example.com" --proxy 127.0.0.1:8080 -k'
The -U option here is bothering. Likewise there are other options which I observed exceptions on. So I am not sure if I am using uncurl correctly or not.
All the curl commands are converted when I used online convertor from curl.trillworks.com.
Can you please provide me the pointer on how to convert curl commands in to python requests?
**** :- I could able to send the curl command using 'runcurl' package. As per my understanding, the uncurl package has limitation of sending the curl request which the runcurl bridges by providing the 'execute' method. I used the following code to do so.
import runcurl
cmd = "curl -v -u "00dcf6e7-4513-4e46-bbaf-ef4cac8f8d47":"XUmh8pIG68Zo" -X GET "https://example.com" -k"
#strip off the curl options like -k, -v, -X, GET which causes runcurl to #throw exception.
try:
code = runcurl.execute(command)
except:
code = 400
//Write the failed cases in some file for further references
As earlier, I have to strip off the options supported by curl like '-k', '-v'. In the above curl command, the option for authentication is provided with the '-u' option. But the runcurl is throwing an exception.
I am seeking help on this point. My expectation is that 'runcurl' should not error out for the required options supported by curl. For ex:- '-u' option. Did I miss anything here? Is there any better way of handling curl command above?
Note :- I am trying to avoid the use of 'subprocess.call()' to call the curl command in my code.
When i use curl under proxy for rest framework api testing in django i'm getting this error Exception Value: Unable to parse connection string: ""
i have used
curl -v --noproxy localhost -X POST -H "Authorization: Token <token id >" -d "email=d#c.co" "http://localhost:4000/api/....."
and also tried
export http_proxy=""
while was working properly when i was not under proxy
curl -X POST -H "Authorization: Token <token id >" -d "email=d#c.co" "http://localhost:4000/api/....."
From the manual of --noproxy, it expects -
Comma-separated list of hosts which do not use a proxy, if one is specified. The only wildcard is a single * character, which matches all hosts, and effectively disables the proxy.
So you must specify a comma separated list of hosts. That may be either specific hosts such as "localhost:4000" or "*".
So the following should work-
curl -v --noproxy "*" localhost -X POST -H "Authorization: Token <token id >" -d "email=d#c.co" "http://localhost:4000/api/....."
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>"})
How can I test the user service from the command-line using curl?
Let's say I have a protoRPC service running and I issue something like:
curl -H 'content-type:application/json' -d '{"name":"test1"}' http://localhost:8080/api.context_create
but this service requires a logged-in user. How can I simulate one?
It looks like you need to do it in two steps (see this link for the details). The first is submitting a request to:
$ curl http://localhost:8000/_ah/login -d "email=youremail#here.com&action=Log+In" -c -
The response will look something like this:
localhost FALSE / FALSE 0 dev_appserver_login "youremail#here.com:False:0123456789101112"
You can then submit a request to your target page, using the cookie provided. In your case (assuming the above cookie) you could try:
curl -H 'content-type:application/json' \
-d '{"name":"test1"}' \
-b "dev_appserver_login="youremail#here.com:False:0123456789101112"" \
http://localhost:8080/api.context_create