|
| 1 | +--- |
| 2 | +name: javascript-proxy-headers |
| 3 | +description: >- |
| 4 | + Send and receive custom headers during HTTPS CONNECT tunneling in Node.js. |
| 5 | + Use when integrating proxy headers with axios, node-fetch, got, undici, ky, |
| 6 | + wretch, make-fetch-happen, needle, superagent, or typed-rest-client. |
| 7 | +--- |
| 8 | + |
| 9 | +# javascript-proxy-headers |
| 10 | + |
| 11 | +Send custom headers to proxies and receive proxy response headers during HTTPS CONNECT tunneling. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +npm install javascript-proxy-headers |
| 17 | +``` |
| 18 | + |
| 19 | +Install your HTTP client as a peer dependency (axios, got, node-fetch, etc.). |
| 20 | + |
| 21 | +## Quick Reference |
| 22 | + |
| 23 | +| Library | Import | Factory | |
| 24 | +|---------|--------|---------| |
| 25 | +| axios | `javascript-proxy-headers/axios` | `createProxyAxios` | |
| 26 | +| node-fetch | `javascript-proxy-headers/node-fetch` | `proxyFetch` | |
| 27 | +| got | `javascript-proxy-headers/got` | `createProxyGot` | |
| 28 | +| undici | `javascript-proxy-headers/undici` | `request` | |
| 29 | +| ky | `javascript-proxy-headers/ky` | `createProxyKy` | |
| 30 | +| wretch | `javascript-proxy-headers/wretch` | `createProxyWretch` | |
| 31 | +| make-fetch-happen | `javascript-proxy-headers/make-fetch-happen` | `createProxyMakeFetchHappen` | |
| 32 | +| needle | `javascript-proxy-headers/needle` | `proxyNeedleGet` | |
| 33 | +| superagent | `javascript-proxy-headers/superagent` | `createProxySuperagent` | |
| 34 | +| typed-rest-client | `javascript-proxy-headers/typed-rest-client` | `createProxyRestClient` | |
| 35 | + |
| 36 | +## Usage Patterns |
| 37 | + |
| 38 | +### axios |
| 39 | + |
| 40 | +```javascript |
| 41 | +import { createProxyAxios } from 'javascript-proxy-headers/axios'; |
| 42 | + |
| 43 | +const client = createProxyAxios({ |
| 44 | + proxy: 'http://user:pass@proxy.example.com:8080', |
| 45 | + proxyHeaders: { 'X-ProxyMesh-Country': 'US' } |
| 46 | +}); |
| 47 | + |
| 48 | +const response = await client.get('https://httpbin.org/ip'); |
| 49 | +console.log(response.headers['x-proxymesh-ip']); |
| 50 | +``` |
| 51 | + |
| 52 | +### node-fetch |
| 53 | + |
| 54 | +```javascript |
| 55 | +import { proxyFetch } from 'javascript-proxy-headers/node-fetch'; |
| 56 | + |
| 57 | +const response = await proxyFetch('https://httpbin.org/ip', { |
| 58 | + proxy: 'http://user:pass@proxy.example.com:8080', |
| 59 | + proxyHeaders: { 'X-ProxyMesh-Country': 'US' } |
| 60 | +}); |
| 61 | + |
| 62 | +console.log(response.proxyHeaders.get('x-proxymesh-ip')); |
| 63 | +``` |
| 64 | + |
| 65 | +### got |
| 66 | + |
| 67 | +```javascript |
| 68 | +import { createProxyGot } from 'javascript-proxy-headers/got'; |
| 69 | + |
| 70 | +const client = createProxyGot({ |
| 71 | + proxy: 'http://user:pass@proxy.example.com:8080', |
| 72 | + proxyHeaders: { 'X-ProxyMesh-Country': 'US' } |
| 73 | +}); |
| 74 | + |
| 75 | +const response = await client('https://httpbin.org/ip'); |
| 76 | +console.log(response.headers['x-proxymesh-ip']); |
| 77 | +``` |
| 78 | + |
| 79 | +### undici |
| 80 | + |
| 81 | +```javascript |
| 82 | +import { request } from 'javascript-proxy-headers/undici'; |
| 83 | + |
| 84 | +const { statusCode, headers, body, proxyHeaders } = await request( |
| 85 | + 'https://httpbin.org/ip', |
| 86 | + { |
| 87 | + proxy: 'http://user:pass@proxy.example.com:8080', |
| 88 | + proxyHeaders: { 'X-ProxyMesh-Country': 'US' } |
| 89 | + } |
| 90 | +); |
| 91 | + |
| 92 | +console.log(proxyHeaders.get('x-proxymesh-ip')); |
| 93 | +``` |
| 94 | + |
| 95 | +### Core Agent (advanced) |
| 96 | + |
| 97 | +For direct control with any Node.js HTTP client: |
| 98 | + |
| 99 | +```javascript |
| 100 | +import { ProxyHeadersAgent } from 'javascript-proxy-headers'; |
| 101 | +import https from 'https'; |
| 102 | + |
| 103 | +const agent = new ProxyHeadersAgent('http://proxy.example.com:8080', { |
| 104 | + proxyHeaders: { 'X-ProxyMesh-Country': 'US' }, |
| 105 | + onProxyConnect: (headers) => { |
| 106 | + console.log('Proxy IP:', headers.get('x-proxymesh-ip')); |
| 107 | + } |
| 108 | +}); |
| 109 | + |
| 110 | +https.get('https://httpbin.org/ip', { agent }, (res) => { |
| 111 | + // Handle response |
| 112 | +}); |
| 113 | +``` |
| 114 | + |
| 115 | +## Proxy Headers |
| 116 | + |
| 117 | +Custom headers sent during CONNECT are proxy-specific. Check your proxy provider's docs. |
| 118 | + |
| 119 | +Example with [ProxyMesh](https://proxymesh.com): |
| 120 | + |
| 121 | +| Header | Direction | Purpose | |
| 122 | +|--------|-----------|---------| |
| 123 | +| `X-ProxyMesh-Country` | Send | Route through specific country | |
| 124 | +| `X-ProxyMesh-IP` | Send/Receive | Request or receive sticky IP | |
| 125 | + |
| 126 | +## Testing |
| 127 | + |
| 128 | +```bash |
| 129 | +export PROXY_URL='http://user:pass@proxy.example.com:8080' |
| 130 | +npm test |
| 131 | +# or specific adapters: |
| 132 | +node test/test_proxy_headers.js -v axios got |
| 133 | +``` |
0 commit comments