user1704863
user1704863

Reputation: 404

HTTP requests.post fails

I'm using the python requests library to get and post http content. I have no problem using the get function but my post function seems to fail or not do anything at all. From my understanding the requests library the POST function automatically encodes the data you send but I'm not sure if that's actually happening

code:

data = 'hash='+hash+'&confirm=Continue+as+Free+User'   
r = requests.post(url,data)
html = r.text

by checking the "value" of html I can tell that the return response is that of the url without the POST.

Upvotes: 0

Views: 2771

Answers (3)

Pompina Singh
Pompina Singh

Reputation: 11

import requests

url = "http://computer-database.herokuapp.com/computers"

payload = "name=Hello11111122OKOK&introduced=1986-12-26&discontinued=2100-12-26&company=13"
headers = {
    'accept-language': "en-US,en;q=0.9,kn;q=0.8",
    'accept-encoding': "gzip, deflate",
    'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    'content-type': "application/x-www-form-urlencoded",
    'cache-control': "no-cache",
    'postman-token': "3e5dabdc-149a-ff4c-a3db-398a7b52f9d5"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)

Upvotes: 0

Max Xu
Max Xu

Reputation: 2549

post(url, data=None, **kwargs)
Sends a POST request. Returns :class:`Response` object.

:param url: URL for the new :class:`Request` object.
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.

Upvotes: 0

Ian Stapleton Cordasco
Ian Stapleton Cordasco

Reputation: 28707

You're not taking advantage of how requests will encode it for you. To do so, you need to write your code this way:

data = {'hash': hash, 'confirm': 'Continue as Free User'}
r = requests.post(url, data)
html = r.text

I can not test this for you but this is how the encoding happens automatically.

Upvotes: 3

Related Questions