|
1 | | -# python-requests |
2 | | -Learn how to use Python Requests module |
| 1 | +# Python Requests Library |
| 2 | + |
| 3 | +[<img src="https://img.shields.io/static/v1?label=&message=Requets&color=brightgreen" />](https://github.com/topics/requests) [<img src="https://img.shields.io/static/v1?label=&message=Python&color=important" />](https://github.com/topics/python) |
| 4 | + |
| 5 | +- [Getting started with Requests](#getting-started-with-requests) |
| 6 | +- [Python requests: GET](#python-requests-get) |
| 7 | +- [Reading responses](#reading-responses) |
| 8 | +- [Using Python request headers](#using-python-request-headers) |
| 9 | +- [Python requests: POST](#python-requests-post) |
| 10 | + |
| 11 | +The **Requests** module is one widely used to send HTTP requests. It’s a third-party alternative to the standard [urllib](https://docs.python.org/3/library/urllib.html), [urllib2](https://docs.python.org/2/library/urllib2.html), and [urllib3](https://urllib3.readthedocs.io/en/latest/) as they can be confusing and often need to be used together. **Requests** in Python greatly simplifies sending HTTP requests to their destination. |
| 12 | + |
| 13 | +This article gives you an overview of everything you need about Requests. |
| 14 | + |
| 15 | +For a detailed explanation, see our [blog post](https://oxylabs.io/blog/python-requests). |
| 16 | + |
| 17 | +## Getting started with Requests |
| 18 | + |
| 19 | +Installing Requests is simple as it can be done through a terminal. |
| 20 | + |
| 21 | +```shell |
| 22 | +$ pip install requests |
| 23 | +``` |
| 24 | + |
| 25 | +Finally, before beginning to use Requests in any project, the library needs to be imported: |
| 26 | + |
| 27 | +```python |
| 28 | +import requests |
| 29 | +``` |
| 30 | + |
| 31 | +## Python requests: GET |
| 32 | + |
| 33 | +To send a GET request, invoke `requests.get()` in Python and add a destination URL, as follows: |
| 34 | + |
| 35 | +```python |
| 36 | +import requests |
| 37 | +requests.get('http://httpbin.org/') |
| 38 | +``` |
| 39 | + |
| 40 | +GET requests can be sent with specific parameters if required: |
| 41 | + |
| 42 | +```python |
| 43 | +payload = {'key1': 'value1', 'key2': 'value2'} |
| 44 | +requests.get('http://httpbin.org/', params=payload) |
| 45 | +``` |
| 46 | + |
| 47 | +## Reading responses |
| 48 | + |
| 49 | +```python |
| 50 | +response = requests.get('http://httpbin.org/') |
| 51 | +print(response.status_code) |
| 52 | + |
| 53 | +# OUTPUT: <Response [200]> |
| 54 | +``` |
| 55 | + |
| 56 | +To read the content of the response, we need to access the text part by using `response.text`: |
| 57 | + |
| 58 | +```python |
| 59 | +print(response.text) |
| 60 | +``` |
| 61 | + |
| 62 | +Responses can also be decoded to the JSON format: |
| 63 | + |
| 64 | +```python |
| 65 | +response = requests.get('http://api.github.com') |
| 66 | +print(response.json()) |
| 67 | +``` |
| 68 | + |
| 69 | +## Using Python request headers |
| 70 | + |
| 71 | +Response headers are another important part of the request: |
| 72 | + |
| 73 | +```python |
| 74 | +print(response.headers) |
| 75 | +``` |
| 76 | + |
| 77 | +You can also send custom Python request headers. To check whether our request header has been sent successfully, we will need to make the call `response.request.headers`: |
| 78 | + |
| 79 | +```python |
| 80 | +import requests |
| 81 | + |
| 82 | +headers = {'user-agent': 'my-agent/1.0.1'} |
| 83 | +response = requests.get('http://httpbin.org/', headers=headers) |
| 84 | +print(response.request.headers) |
| 85 | +``` |
| 86 | + |
| 87 | +## Python requests: POST |
| 88 | + |
| 89 | +Sending a POST request is *almost* as simple as sending a GET: |
| 90 | + |
| 91 | +```python |
| 92 | +response = requests.post('https://httpbin.org/post', data = {'key':'value'}) |
| 93 | +``` |
| 94 | + |
| 95 | +The requests library accepts arguments from dictionary objects which can be utilized to send more advanced data: |
| 96 | + |
| 97 | +```python |
| 98 | +payload = {'key1': 'value1', 'key2': 'value2'} |
| 99 | +response = requests.post('https://httpbin.org/post', data = payload) |
| 100 | +``` |
| 101 | + |
| 102 | +Requests have an added feature that automatically converts the POST request data into JSON. |
| 103 | + |
| 104 | +```python |
| 105 | +import requests |
| 106 | + |
| 107 | +payload = {'key1': 'value1', 'key2': 'value2'} |
| 108 | +response = requests.post('https://httpbin.org/post', json = payload) |
| 109 | +print(response.json()) |
| 110 | +``` |
| 111 | + |
| 112 | +Alternatively, the *json* library might be used to convert dictionaries into JSON objects: |
| 113 | + |
| 114 | +```python |
| 115 | +import json |
| 116 | +import requests |
| 117 | + |
| 118 | +payload = { |
| 119 | + 'key1': 'value1', |
| 120 | + 'key2': 'value2'} |
| 121 | +jsonData = json.dumps(payload) |
| 122 | +response = requests.post('https://httpbin.org/post', data = jsonData) |
| 123 | +print(response.json()) |
| 124 | +``` |
| 125 | + |
| 126 | +If you wish to learn more about Requests, see our [blog post](https://oxylabs.io/blog/python-requests). |
0 commit comments