How to handle special characters in python requests post request - python

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)

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.

Curl request is working fine in terminal but not working in python after converting

I have a curl request that gets me the '200' response on terminal but when I convert it to python using 'https://curl.trillworks.com/' and send python request using terminal I'm getting '403' as a response.
Curl Request
curl 'https://www.realestate.com.au/agent/graphql' --data-binary $'{"operationName":"SendEnquiry","variables":{"enquiry":{"id":"1375705","enquiryType":"General enquiry","propertyAddress":"","message":"Australia property prices?","contactMethod":"EMAIL","name":"sadfal","phone":"","email":"ahmadarshi#ucp.edu.pk","sourceUrl":"https://www.realestate.com.au/agent/mary-wang-1375705","referrer":""}},"query":"mutation SendEnquiry($enquiry: ConsumerEnquiryInput\u21) {\\n sendEnquiry(enquiry: $enquiry) {\\n status\\n isValid\\n message\\n __typename\\n }\\n}\\n"}' --compressed
Python Request
import requests
data = '${"operationName":"SendEnquiry","variables":{"enquiry":{"id":"1375705","enquiryType":"General enquiry","propertyAddress":"","message":"Australia property prices?","contactMethod":"EMAIL","name":"sadfal","phone":"","email":"ahmadarshi#ucp.edu.pk","sourceUrl":"https://www.realestate.com.au/agent/mary-wang-1375705","referrer":""}},"query":"mutation SendEnquiry($enquiry: ConsumerEnquiryInput\\u21) {\\\\n sendEnquiry(enquiry: $enquiry) {\\\\n status\\\\n isValid\\\\n message\\\\n __typename\\\\n }\\\\n}\\\\n"}'
response = requests.post('https://www.realestate.com.au/agent/graphql', data=data)
403 means that the request is denied (you are forbidden to access the page). Make sure that you validate with the API documentation what headers are required for the request and potentially what Content-Type needs to be specified along with the data.
Also, it seems that you might be malforming the request - the $ should likely be excluded from the string.
it appears to be a bug in the https://curl.trillworks.com/ bash parser - the $ is not passed to curl and is not part of data to be posted. i'd send a bugreport to the trillworks guys if i were you. quoting http://mywiki.wooledge.org/Quotes :
$'...' : This is a Bash extension. It prevents everything except backslash escaping, and also allows special backslash escape sequences like \n for newline, \t for tab, and \xnn for bytes specified in hexadecimal.
and the curl.trillworks.com parser incorrectly parses the $'...' syntax.

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

Converting url encode data from curl to json object in python using requests

What is the best way to convert the below curl post into python request using the requests module:
curl -X POST https://api.google.com/gmail --data-urlencode json='{"user": [{"message":"abc123", "subject":"helloworld"}]}'
I tried using python requests as below, but it didn't work:
payload = {"user": [{"message":"abc123", "subject":"helloworld"}]}
url = https://api.google.com/gmail
requests.post(url, data=json.dumps(payload),auth=(user, password))
Can anybody help.
As the comment mentioned, you should put your url variable string in quotes "" first.
Otherwise, your question is not clear. What errors are being thrown and/or behavior is happening?
New cURL method in Python

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.

Categories

Resources