|
| 1 | +# axios |
| 2 | + |
| 3 | +[axios](https://axios-http.com/) is the most popular HTTP client for JavaScript. This page describes how to use axios with proxies and how to send and receive custom proxy headers. |
| 4 | + |
| 5 | +## Getting Started |
| 6 | + |
| 7 | +### Prerequisites |
| 8 | + |
| 9 | +Install the packages: |
| 10 | + |
| 11 | +```bash |
| 12 | +npm install javascript-proxy-headers axios |
| 13 | +``` |
| 14 | + |
| 15 | +### Quick Example |
| 16 | + |
| 17 | +```javascript |
| 18 | +import { createProxyAxios } from 'javascript-proxy-headers/axios'; |
| 19 | + |
| 20 | +// Create a configured axios instance |
| 21 | +const client = await createProxyAxios({ |
| 22 | + proxy: 'http://user:pass@proxy.example.com:8080', |
| 23 | + proxyHeaders: { 'X-ProxyMesh-Country': 'US' } |
| 24 | +}); |
| 25 | + |
| 26 | +// Make requests |
| 27 | +const response = await client.get('https://httpbin.org/ip'); |
| 28 | + |
| 29 | +// Access response data |
| 30 | +console.log(response.data); |
| 31 | + |
| 32 | +// Access proxy response headers (merged into response.headers) |
| 33 | +console.log(response.headers['x-proxymesh-ip']); |
| 34 | +``` |
| 35 | + |
| 36 | +## API Reference |
| 37 | + |
| 38 | +### createProxyAxios(options) |
| 39 | + |
| 40 | +Creates an axios instance configured with proxy header support. |
| 41 | + |
| 42 | +**Parameters:** |
| 43 | + |
| 44 | +| Name | Type | Description | |
| 45 | +|------|------|-------------| |
| 46 | +| `options.proxy` | `string` | Proxy URL (e.g., `http://user:pass@proxy:8080`) | |
| 47 | +| `options.proxyHeaders` | `Object` | Headers to send to the proxy | |
| 48 | +| `options.onProxyConnect` | `Function` | Callback when CONNECT completes: `(headers) => void` | |
| 49 | +| `options.axiosOptions` | `Object` | Additional axios instance options | |
| 50 | + |
| 51 | +**Returns:** `Promise<AxiosInstance>` - Configured axios instance |
| 52 | + |
| 53 | +**Example:** |
| 54 | + |
| 55 | +```javascript |
| 56 | +import { createProxyAxios } from 'javascript-proxy-headers/axios'; |
| 57 | + |
| 58 | +const client = await createProxyAxios({ |
| 59 | + proxy: 'http://proxy:8080', |
| 60 | + proxyHeaders: { |
| 61 | + 'X-ProxyMesh-Country': 'US', |
| 62 | + 'X-ProxyMesh-Session': 'my-session' |
| 63 | + }, |
| 64 | + onProxyConnect: (headers) => { |
| 65 | + console.log('Connected via:', headers.get('x-proxymesh-ip')); |
| 66 | + }, |
| 67 | + axiosOptions: { |
| 68 | + timeout: 30000, |
| 69 | + headers: { 'User-Agent': 'MyApp/1.0' } |
| 70 | + } |
| 71 | +}); |
| 72 | +``` |
| 73 | + |
| 74 | +### Convenience Functions |
| 75 | + |
| 76 | +For one-off requests, you can use the convenience functions: |
| 77 | + |
| 78 | +```javascript |
| 79 | +import { get, post } from 'javascript-proxy-headers/axios'; |
| 80 | + |
| 81 | +// GET request |
| 82 | +const response = await get('https://httpbin.org/ip', { |
| 83 | + proxy: 'http://proxy:8080', |
| 84 | + proxyHeaders: { 'X-ProxyMesh-Country': 'US' } |
| 85 | +}); |
| 86 | + |
| 87 | +// POST request |
| 88 | +const response = await post('https://httpbin.org/post', { key: 'value' }, { |
| 89 | + proxy: 'http://proxy:8080', |
| 90 | + proxyHeaders: { 'X-ProxyMesh-Country': 'US' } |
| 91 | +}); |
| 92 | +``` |
| 93 | + |
| 94 | +## Accessing Proxy Headers |
| 95 | + |
| 96 | +Proxy response headers are automatically merged into `response.headers`: |
| 97 | + |
| 98 | +```javascript |
| 99 | +const response = await client.get('https://httpbin.org/ip'); |
| 100 | + |
| 101 | +// Proxy headers are available in response.headers |
| 102 | +const proxyIp = response.headers['x-proxymesh-ip']; |
| 103 | +const proxyCountry = response.headers['x-proxymesh-country']; |
| 104 | +``` |
| 105 | + |
| 106 | +You can also access the underlying agent to get the last proxy headers: |
| 107 | + |
| 108 | +```javascript |
| 109 | +const client = await createProxyAxios({ |
| 110 | + proxy: 'http://proxy:8080', |
| 111 | + proxyHeaders: { 'X-ProxyMesh-Country': 'US' } |
| 112 | +}); |
| 113 | + |
| 114 | +await client.get('https://httpbin.org/ip'); |
| 115 | + |
| 116 | +// Access via the agent |
| 117 | +console.log(client.proxyAgent.lastProxyHeaders); |
| 118 | +``` |
| 119 | + |
| 120 | +## All Request Methods |
| 121 | + |
| 122 | +The created axios instance supports all standard methods: |
| 123 | + |
| 124 | +```javascript |
| 125 | +const client = await createProxyAxios({ |
| 126 | + proxy: 'http://proxy:8080', |
| 127 | + proxyHeaders: { 'X-ProxyMesh-Country': 'US' } |
| 128 | +}); |
| 129 | + |
| 130 | +// GET |
| 131 | +const r1 = await client.get('https://api.example.com/data'); |
| 132 | + |
| 133 | +// POST |
| 134 | +const r2 = await client.post('https://api.example.com/data', { key: 'value' }); |
| 135 | + |
| 136 | +// PUT |
| 137 | +const r3 = await client.put('https://api.example.com/data/1', { key: 'updated' }); |
| 138 | + |
| 139 | +// DELETE |
| 140 | +const r4 = await client.delete('https://api.example.com/data/1'); |
| 141 | + |
| 142 | +// PATCH |
| 143 | +const r5 = await client.patch('https://api.example.com/data/1', { key: 'patched' }); |
| 144 | + |
| 145 | +// HEAD |
| 146 | +const r6 = await client.head('https://api.example.com/data'); |
| 147 | +``` |
| 148 | + |
| 149 | +## Using with Existing Code |
| 150 | + |
| 151 | +If you have existing axios code, you can create a configured instance and use it as a drop-in replacement: |
| 152 | + |
| 153 | +```javascript |
| 154 | +// Before (standard axios) |
| 155 | +import axios from 'axios'; |
| 156 | +const response = await axios.get('https://api.example.com/data'); |
| 157 | + |
| 158 | +// After (with proxy headers) |
| 159 | +import { createProxyAxios } from 'javascript-proxy-headers/axios'; |
| 160 | +const axios = await createProxyAxios({ |
| 161 | + proxy: process.env.PROXY_URL, |
| 162 | + proxyHeaders: { 'X-ProxyMesh-Country': 'US' } |
| 163 | +}); |
| 164 | +const response = await axios.get('https://api.example.com/data'); |
| 165 | +``` |
| 166 | + |
| 167 | +## Error Handling |
| 168 | + |
| 169 | +```javascript |
| 170 | +import { createProxyAxios } from 'javascript-proxy-headers/axios'; |
| 171 | + |
| 172 | +const client = await createProxyAxios({ |
| 173 | + proxy: 'http://proxy:8080', |
| 174 | + proxyHeaders: { 'X-ProxyMesh-Country': 'US' } |
| 175 | +}); |
| 176 | + |
| 177 | +try { |
| 178 | + const response = await client.get('https://httpbin.org/ip'); |
| 179 | + console.log(response.data); |
| 180 | +} catch (error) { |
| 181 | + if (error.name === 'ConnectError') { |
| 182 | + // Proxy CONNECT failed |
| 183 | + console.error('Proxy error:', error.statusCode, error.statusMessage); |
| 184 | + console.error('Proxy headers:', error.proxyHeaders); |
| 185 | + } else if (error.response) { |
| 186 | + // Target server error |
| 187 | + console.error('Server error:', error.response.status); |
| 188 | + } else { |
| 189 | + // Network error |
| 190 | + console.error('Network error:', error.message); |
| 191 | + } |
| 192 | +} |
| 193 | +``` |
0 commit comments