Python requests not working while cURL works - python

I'm trying to automate backups in Adobe Experience Manager.
I can do it from jmx console and I got cURL from there when I called a function to start backup.
I tried running the same curl from the terminal on my laptop and it worked fine as well.
Next I tried to do the same thing in python using requests however I keep getting 404 error.
Here's the original curl:
curl -u admin:admin -X POST http://localhost:4502/system/console/jmx/com.adobe.granite:type\=Repository/op/startBackup/java.lang.String\?target\=backup_test.zip
This works fine. Here's my python code:
import requests
URL = "http://localhost:4502/system/console/jmx/com.adobe.granite:type\=Repository/op/startBackup/java.lang.String\?target\=backup_test.zip"
resp = requests.post(URL, auth=('admin', 'admin'))
print(resp.content)
When running the above I get this error:
b'<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>\n<title>Error 404 </title>\n</head>\n<body>\n<h2>HTTP ERROR: 404</h2>\n<
p>Problem accessing /system/console/jmx/com.adobe.granite:type%5C=Repository/op/startBackup/java.lang.String%5C. Reason:\n<pre> Not Found</pre></p>\n<hr />\n</b
ody>\n</html>\n'
When using this instead of full url:
URL = "http://localhost:4502/system/console"
I get 200.
What could be wrong with this?

Related

Bad JSON formatting in Jupyter API PUT call - error 400

I am quite new in python and can't understand why I am getting this 400 error. It seems like JSON formatting is wrong in the PUT call.
I have this dataframe.
And I am trying to run this code
url = "https://api.name.com/pacing-strategy"
for row in df_call_put.itertuples():
qs_li_id = {'line_item_id':row.line_item_id, 'member_id':1111}
payload_li_ps = [{'line_item_id':row.line_item_id,
'pacing_strategies':
[{'strategy_type': 'intelligent',
'when': 'ALWAYS'}]}]
rs_li_ps = requests.put(url, data=json.dumps(payload_li_ps) ,headers=headers, params=qs_li_id)
It should be just fine (exactly formatted like other PUT call I use) but it returns "
'errorCode': 400, 'errorDescription': 'Parameter line_item_id is invalid.'
As a comparison I ran the exact same thing in postman and it works fine, I however need it in a script so I have to use it in Jupyter.
One lead that could help you to find the solution. In Terminal I managed to make it work switching code from
curl -b cookies -c cookies -X PUT -H 'Content-Type: application/json' -d #always_asap.json "https://api.name.com/pacing-strategy?line_item_id=12345678"`
to
curl -b cookies -c cookies -X PUT -d #always_asap.json 'https://api.name.com/pacing-strategy?line_item_id=12345678&member_id=1111'
So it seems like the JSON reformatting with Content type was the issue.
Thanks!
I tried to remove json.dumps in the PUT call
I tried to change payload_li_ps code removing [] at the beginning.
We should get a OK status 200 but we have a 400 error

zip file upload with Python Requests Not working

Hi am attempting to upload a file to a server when making a post with requests but everytime I get errors. If do the same with cURL it goes through but Im not familiar with cURL or uploading files really, mostly get requests, so I have no clue what its doing differently. I am on windows and am running the below. This is being uploaded to mcafee epo so Im not sure if their api just super picky or what the difference is but every python example ive tried for uploading a file via requests module has failed for me.
url = "https://server.url.com:1234/remote/repository.checkInPackage.do?&allowUnsignedPackages=True&option=Normal&branch=Evaluation"
user = "domain\user"
password = "mypass"
filepath = "C:\\my\\folder\\with\\afile.zip"
with open(filepath, "rb") as f:
file_dict = {"file": f}
response = requests.post(url, auth=(user, password), files=file_dict)
I usually get a error as follows:
'Error 0 :\r\njava.lang.reflect.InvocationTargetException\r\n'
if I use cURL it works though
curl.exe -k -s -u "domain\username:mypass" "https://server.url.com:1234/remote/repository.checkInPackage.do?&allowUnsignedPackages=True&option=Normal&branch=Evaluation" -F file=#"C:\my\folder\with\afile.zip"
I cant really see the difference though and am wondering what is being done differently on the backend for cURL or what I could be doing wrong when using python.

How to handle special characters in python requests post request

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)

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.

Curl works but python requests doesn't

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

Categories

Resources