I can't seem to Google it, but I want a function that does this:
Accept 3 arguments (or more, whatever):
Return me the results, and the response code.
Is there a snippet that does this?
The post() method sends a POST request to the specified url. The post() method is used when you want to send some data to the server.
Python is only a language, to get GET and POST data, you need a web framework or toolkit written in Python. Django is one, as Charlie points out, the cgi and urllib standard modules are others. Also available are Turbogears, Pylons, CherryPy, web.py, mod_python, fastcgi, etc, etc.
POST request in itself means sending information in the body. I found a fairly simple way to do this. Use Postman by Google, which allows you to specify the content-type (a header field) as application/json and then provide name-value pairs as parameters. Just use your URL in the place of theirs.
requests
https://github.com/kennethreitz/requests/
Here's a few common ways to use it:
import requests url = 'https://...' payload = {'key1': 'value1', 'key2': 'value2'} # GET r = requests.get(url) # GET with params in URL r = requests.get(url, params=payload) # POST with form-encoded data r = requests.post(url, data=payload) # POST with JSON import json r = requests.post(url, data=json.dumps(payload)) # Response, status etc r.text r.status_code
httplib2
https://github.com/jcgregorio/httplib2
>>> from httplib2 import Http >>> from urllib import urlencode >>> h = Http() >>> data = dict(name="Joe", comment="A test comment") >>> resp, content = h.request("http://bitworking.org/news/223/Meet-Ares", "POST", urlencode(data)) >>> resp {'status': '200', 'transfer-encoding': 'chunked', 'vary': 'Accept-Encoding,User-Agent', 'server': 'Apache', 'connection': 'close', 'date': 'Tue, 31 Jul 2007 15:29:52 GMT', 'content-type': 'text/html'}
Even easier: via the requests module.
import requests get_response = requests.get(url='http://google.com') post_data = {'username':'joeb', 'password':'foobar'} # POST some form-encoded data: post_response = requests.post(url='http://httpbin.org/post', data=post_data)
To send data that is not form-encoded, send it serialised as a string (example taken from the documentation):
import json post_response = requests.post(url='http://httpbin.org/post', data=json.dumps(post_data)) # If using requests v2.4.2 or later, pass the dict via the json parameter and it will be encoded directly: post_response = requests.post(url='http://httpbin.org/post', json=post_data)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With