I was making a POST request using requests module in python. From the value pair of the response.json(The response from POST request), the value contains multiple lines. I can see the entire response.json while making CURL POST request. But, for requests module I cannot see the entire value which has multiple lines in it.
How could I get the same response as from the curl?
This is the response using CURL POST request
{"output":"b171_L19# show cmd status \n SSH and sudo pwdless\t:Enable\nAutostrt\t\t\t:Enabled\nCmd status\t\t\t:Running\ncmd version\t\t\t:4.1.2\n additional status\t\t:normal\n"}
This is the response using requests module in python
{"output":"b171_L19# \n"}
Related
I'm trying to scrape a web site. Before writing the code, I copied the request from the browser's network tool as a curl and pasted it into Insomnia (like Postman). I could resend the same request with an 200 response in Insomnia.
However, when I mimic the same request in a scrapy Request with the same body, url and header; Scrapy receives 403 responses. On the other hand, if I populate a Python code with requests library through Insomnia; the identical request works in requests library.
So how could two identical requests sent in Scrapy and Requests have different responses, that is, the one Scrapy send fails whereas the one Requests sents succeeds?
Thank you
I want to create a POST request to a site like mydomain.com?character=%61 using Requests library.
But if I use requests.post(url) with url = 'mydomain.com?character=%61' then it sends a POST request to mydomain.com?character=a. How can I keep %61 in request instead of decoding it to a?
Thanks all!
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)
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.
I posted another question but made a bit of a mess of it in the comments section. Basically, I am trying to use the requests library in Python in order to accomplish what I normally would by using CURL in order to process a GET request to an API. From what I have learned from a very helpful person here, I can process the request, as well as the authorisation header by doing the following:
Original CURL Command that I would normally use:
curl -X GET -H 'Authorization: exapi:111:58d351234e1a:LA2' 'http://api.example.com/v1.14/user?id=1234'
This is the Python code I am using for my script:
import requests
import json
url = "http://api.example.com/v1.14/user?id=1234"
headers = {"Authorization": "exapi:111:58d351234e1a:LA2"}
response = requests.get(url, headers=headers)
print response.json
However, when I run my code, I receive bound method response.json of <response [200]> instead of data that I was expecting from the GET. Can someone help me figure out what I am doing wrong here? I am guessing that I am doing something wrong with my header but I am not sure.
As #juanpa.arrivilaga has already mentioned and as the printed message clearly says, you need to call the bound json method. The source of confusion is likely from content, which is an attribute.
response.json() # method
response.content # attribute
How about using json module explicitly:
data = json.loads(response.text)
print data