Skip to content

Commit 36061bf

Browse files
mrcsnv0x676e67
andauthored
Enhanced Quickstart and Basic usage pages (#576)
* Enhance proxy usage documentation with the correct usage. Expanded the proxy usage documentation to include authentication, per-request proxies with custom headers, and clarified Unix socket proxy usage. * Revise proxy example in quickstart documentation Updated the proxy usage example to reflect the new Client initialization with a list of proxies. * Correct bullet point formatting in quickstart.md Fix formatting of bullet points in quickstart.md * Update quickstart guide for wreq usage More context added. And anchoring to API references for easy navigation between classes and models. To learn how to use it better * Revise request examples in basic guide Updated examples for GET and POST requests, including form data and headers. Improved clarity and consistency in code snippets. * Change print statement to use response.status --------- Co-authored-by: 0x676e67 <gngppz@gmail.com>
1 parent 030c6c1 commit 36061bf

3 files changed

Lines changed: 260 additions & 187 deletions

File tree

docs/source/getting-started/quickstart.md

Lines changed: 128 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
# :zap: Quick Start
1+
# Quickstart
22

3-
This page will help you get up and running with wreq in minutes.
3+
This page covers the basics of making HTTP requests with wreq. By the end, you will be able to send requests, read responses, pass headers, work with JSON, and route traffic through a proxy.
44

55
---
66

7-
## Basic GET Request
7+
## Making a Request
8+
9+
wreq supports both async and blocking usage. The async client is the default and recommended approach for most use cases.
810

911
=== "Async"
1012
```python
@@ -13,61 +15,88 @@ This page will help you get up and running with wreq in minutes.
1315

1416
async def main():
1517
client = Client()
16-
resp = await client.get("https://httpbin.org/get")
17-
print(resp.status_code)
18-
print(await resp.text())
18+
response = await client.get("https://httpbin.org/get")
19+
print(response.status)
1920

2021
asyncio.run(main())
2122
```
23+
2224
=== "Blocking"
2325
```python
2426
from wreq.blocking import Client
2527

2628
client = Client()
27-
resp = client.get("https://httpbin.org/get")
28-
print(resp.status_code)
29-
print(resp.text())
29+
response = client.get("https://httpbin.org/get")
30+
print(response.status_code)
3031
```
3132

33+
The same interface works for all standard HTTP methods:
34+
35+
```python
36+
response = await client.post("https://httpbin.org/post")
37+
response = await client.put("https://httpbin.org/put")
38+
response = await client.delete("https://httpbin.org/delete")
39+
response = await client.head("https://httpbin.org/get")
40+
```
41+
3242
---
3343

34-
## POST with JSON
44+
## Passing Parameters in URLs
45+
46+
To append query parameters to a URL, pass a dictionary to the `params` argument:
3547

3648
```python
37-
import asyncio
38-
from wreq import Client
49+
params = {"search": "wreq", "page": "1"}
50+
response = await client.get("https://httpbin.org/get", params=params)
51+
print(response.url)
52+
# https://httpbin.org/get?search=wreq&page=1
53+
```
3954

40-
async def main():
41-
client = Client()
42-
data = {"name": "John", "age": 30}
43-
resp = await client.post("https://httpbin.org/post", json=data)
44-
result = await resp.json()
45-
print(result)
55+
---
56+
57+
## Reading the Response
58+
59+
### Status code
4660

47-
asyncio.run(main())
61+
```python
62+
response = await client.get("https://httpbin.org/get")
63+
print(response.status_code)
64+
# 200
4865
```
4966

50-
---
67+
### Text
68+
69+
```python
70+
text = await response.text()
71+
print(text)
72+
```
5173

52-
## Emulation
74+
### JSON
5375

54-
Emulate different browsers to bypass detection:
76+
If the server returns a JSON body, parse it directly with `.json()`:
5577

5678
```python
57-
import asyncio
58-
from wreq import Client, Emulation
79+
response = await client.get("https://httpbin.org/json")
80+
data = await response.json()
81+
print(data)
82+
```
5983

60-
async def main():
61-
client = Client(emulation=Emulation.Safari26)
62-
resp = await client.get("https://tls.peet.ws/api/all")
63-
print(await resp.text())
84+
### Response headers
6485

65-
asyncio.run(main())
86+
Response headers are available as a dictionary-like object:
87+
88+
```python
89+
print(response.headers["content-type"])
90+
# application/json
6691
```
6792

6893
---
6994

70-
## Using Proxies
95+
## Sending Data
96+
97+
### JSON body
98+
99+
Pass a dictionary to the `json` argument and wreq will serialize it and set the correct `Content-Type` header automatically:
71100

72101
The [Proxy](../guide/proxy.md) object defines how your HTTP client routes traffic through a proxy server.
73102
You create one using a named constructor that specifies the scope:
@@ -79,35 +108,87 @@ You create one using a named constructor that specifies the scope:
79108
Once created, you pass it to the `Client` and all requests will go through it automatically.
80109

81110
```python
82-
import asyncio
83-
from wreq import Client, Proxy
111+
payload = {"name": "John", "age": 30}
112+
response = await client.post("https://httpbin.org/post", json=payload)
113+
result = await response.json()
114+
print(result)
115+
```
116+
117+
### Form-encoded data
84118

85-
async def main():
86-
client = Client(proxies=[Proxy.all("http://proxy.example.com:8080")])
87-
resp = await client.get("https://httpbin.org/ip")
88-
print(await resp.text())
119+
To send HTML form data, use the `data` argument instead:
89120

90-
asyncio.run(main())
121+
```python
122+
form = {"username": "john", "password": "secret"}
123+
response = await client.post("https://httpbin.org/post", data=form)
91124
```
92125

93126
---
94127

95128
## Custom Headers
96129

130+
Pass a [HeaderMap](../api/header.md?h=HeaderMap#wreq.header.HeaderMap) to attach additional headers to a request:
131+
97132
```python
98-
import asyncio
99-
from wreq import Client
100133
from wreq.header import HeaderMap
101134

102-
async def main():
103-
client = Client()
104-
headers = HeaderMap()
105-
headers["User-Agent"] = "MyApp/1.0"
106-
headers["Custom-Header"] = "value"
107-
resp = await client.get("https://httpbin.org/headers", headers=headers)
108-
print(await resp.text())
135+
headers = HeaderMap()
136+
headers["User-Agent"] = "MyApp/1.0"
137+
headers["Accept"] = "application/json"
138+
139+
response = await client.get("https://httpbin.org/headers", headers=headers)
140+
print(await response.text())
141+
```
142+
143+
---
144+
145+
## Using Proxies
146+
147+
The [Proxy](../guide/proxy.md) object controls how the client routes traffic. You create one using a named constructor that defines its scope:
148+
149+
- `Proxy.all(url)` routes all traffic through the proxy.
150+
- `Proxy.http(url)` only intercepts plain HTTP requests.
151+
- `Proxy.https(url)` only intercepts HTTPS requests.
152+
153+
Pass the proxy to the `Client` and every subsequent request will use it:
154+
155+
```python
156+
from wreq import Client, Proxy
157+
158+
client = Client(proxies=[Proxy.all("http://proxy.example.com:8080")])
159+
response = await client.get("https://httpbin.org/ip")
160+
print(await response.text())
161+
```
162+
163+
---
164+
165+
## Browser Emulation
166+
167+
wreq can emulate the TLS fingerprint and headers of real browsers, which is useful when connecting to servers that inspect these signals. Pass an [Emulation](../guide/emulation.md) preset to the `Client`:
168+
169+
```python
170+
from wreq import Client, Emulation
171+
172+
client = Client(emulation=Emulation.Safari26)
173+
response = await client.get("https://tls.peet.ws/api/all")
174+
print(await response.text())
175+
```
176+
177+
Available presets are listed in the [Emulation reference](../getting-started/introduction.md#behavior).
178+
179+
---
180+
181+
## Error Handling
182+
183+
Check the status code manually, or call `raise_for_status()` to raise an exception on any 4xx or 5xx response:
184+
185+
```python
186+
response = await client.get("https://httpbin.org/status/404")
109187

110-
asyncio.run(main())
188+
try:
189+
response.raise_for_status()
190+
except Exception as exc:
191+
print(f"Request failed with status {response.status_code}")
111192
```
112193

113194
---

0 commit comments

Comments
 (0)