# Headers, query and body

> How to send headers, query parameters, JSON, forms and binary bodies with POST /v1/requests, with limits and examples.

Source: https://docs.virginproxy.com/headers/

Everything a browser or a program can send, you can send through VirginProxy: headers, query parameters and a body as JSON, as a form or as raw bytes.

## Headers

`headers` is an object of names and string values. They replace the headers of the chosen [browser](https://docs.virginproxy.com/browsers/) with the same name; case does not matter. An empty value leaves a header out.

**cURL**

```bash
curl https://api.virginproxy.com/v1/requests \
  -H "Authorization: Bearer $VP_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://httpbin.org/headers",
    "headers": {
      "Authorization": "Bearer my-token",
      "X-Requested-With": "XMLHttpRequest",
      "Accept": "application/json"
    }
  }'
```

**Python**

```python
import os, requests

r = requests.post(
    "https://api.virginproxy.com/v1/requests",
    headers={"Authorization": "Bearer " + os.environ["VP_KEY"]},
    json={
        "url": "https://httpbin.org/headers",
        "headers": {
            "Authorization": "Bearer my-token",
            "X-Requested-With": "XMLHttpRequest",
            "Accept": "application/json",
        },
    },
    timeout=180,
)
data = r.json()
if not r.ok:
    raise SystemExit(data["error"]["code"] + ": " + data["error"]["message"])
print(data["body"])
```

**Node.js**

```javascript
const res = await fetch("https://api.virginproxy.com/v1/requests", {
  method: "POST",
  headers: { Authorization: "Bearer " + process.env.VP_KEY, "Content-Type": "application/json" },
  body: JSON.stringify({
    url: "https://httpbin.org/headers",
    headers: {
      Authorization: "Bearer my-token",
      "X-Requested-With": "XMLHttpRequest",
      Accept: "application/json",
    },
  }),
  signal: AbortSignal.timeout(180_000),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error.code + ": " + data.error.message);
console.log(data.body);
```

**Python SDK**

```pysdk
from virginproxy import VirginProxy  # docs.virginproxy.com/sdk/virginproxy.py

vp = VirginProxy()  # reads VP_KEY
data = vp.request({
    "url": "https://httpbin.org/headers",
    "headers": {
        "Authorization": "Bearer my-token",
        "X-Requested-With": "XMLHttpRequest",
        "Accept": "application/json",
    },
})
print(data)
```

**Node SDK**

```nodesdk
import { VirginProxy } from "./virginproxy.mjs"; // docs.virginproxy.com/sdk/virginproxy.mjs

const vp = new VirginProxy(); // reads VP_KEY
const data = await vp.request({
  url: "https://httpbin.org/headers",
  headers: {
    Authorization: "Bearer my-token",
    "X-Requested-With": "XMLHttpRequest",
    Accept: "application/json",
  },
});
console.log(data);
```

**PHP**

```php
<?php
$ch = curl_init("https://api.virginproxy.com/v1/requests");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 180,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer " . getenv("VP_KEY"),
        "Content-Type: application/json",
    ],
    CURLOPT_POSTFIELDS => json_encode([
        "url" => "https://httpbin.org/headers",
        "headers" => [
            "Authorization" => "Bearer my-token",
            "X-Requested-With" => "XMLHttpRequest",
            "Accept" => "application/json",
        ],
    ], JSON_UNESCAPED_SLASHES),
]);
$data = json_decode(curl_exec($ch), true);
if (isset($data["error"])) {
    exit($data["error"]["code"] . ": " . $data["error"]["message"] . PHP_EOL);
}
echo $data["body"], PHP_EOL;
```

**Go**

```go
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"
)

func main() {
	payload := []byte(`{
  "url": "https://httpbin.org/headers",
  "headers": {
    "Authorization": "Bearer my-token",
    "X-Requested-With": "XMLHttpRequest",
    "Accept": "application/json"
  }
}`)
	req, err := http.NewRequest("POST", "https://api.virginproxy.com/v1/requests", bytes.NewReader(payload))
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("Authorization", "Bearer "+os.Getenv("VP_KEY"))
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{Timeout: 180 * time.Second}
	res, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()

	var data map[string]any
	if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
		log.Fatal(err)
	}
	if res.StatusCode >= 300 {
		e := data["error"].(map[string]any)
		log.Fatalf("%v: %v", e["code"], e["message"])
	}
	fmt.Println(data["body"])
}
```

POST`/v1/requests`[≈ 1 credit](https://docs.virginproxy.com/billing/#cost)[Open in Playground](https://dash.virginproxy.com/playground#request=eyJ1cmwiOiJodHRwczovL2h0dHBiaW4ub3JnL2hlYWRlcnMiLCJoZWFkZXJzIjp7IkF1dGhvcml6YXRpb24iOiJCZWFyZXIgbXktdG9rZW4iLCJYLVJlcXVlc3RlZC1XaXRoIjoiWE1MSHR0cFJlcXVlc3QiLCJBY2NlcHQiOiJhcHBsaWNhdGlvbi9qc29uIn19)

| Limit | Value |
| --- | --- |
| Number of headers | up to 100 |
| Name | up to 64 characters: letters, digits and `` ! # $ % & ' * + . ^ _ ` \| ~ - `` |
| Value | a string of up to 8,000 characters without line breaks |

`Host`, `Content-Length`, `Connection`, `Transfer-Encoding`, `Upgrade`, `TE`, `Trailer`, `Keep-Alive`, `Proxy-Authorization`, `Proxy-Connection` are managed by the connection and dropped if you send them. Cookies can go in a `Cookie` header or in `cookies`; see [Cookies and sessions](https://docs.virginproxy.com/cookies/).

## Query parameters

`params` adds parameters to the query string of `url`, after those it already has. Values are encoded for you. A list repeats the name.

**cURL**

```bash
curl https://api.virginproxy.com/v1/requests \
  -H "Authorization: Bearer $VP_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://httpbin.org/get?page=1",
    "params": {
      "q": "blue shoes",
      "size": [
        42,
        43
      ],
      "in_stock": true
    }
  }'
```

**Python**

```python
import os, requests

r = requests.post(
    "https://api.virginproxy.com/v1/requests",
    headers={"Authorization": "Bearer " + os.environ["VP_KEY"]},
    json={
        "url": "https://httpbin.org/get?page=1",
        "params": {"q": "blue shoes", "size": [42, 43], "in_stock": True},
    },
    timeout=180,
)
data = r.json()
if not r.ok:
    raise SystemExit(data["error"]["code"] + ": " + data["error"]["message"])
print(data["final_url"])  # https://httpbin.org/get?page=1&q=blue+shoes&size=42&size=43&in_stock=true
```

**Node.js**

```javascript
const res = await fetch("https://api.virginproxy.com/v1/requests", {
  method: "POST",
  headers: { Authorization: "Bearer " + process.env.VP_KEY, "Content-Type": "application/json" },
  body: JSON.stringify({
    url: "https://httpbin.org/get?page=1",
    params: { q: "blue shoes", size: [42, 43], in_stock: true },
  }),
  signal: AbortSignal.timeout(180_000),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error.code + ": " + data.error.message);
console.log(data.final_url); // https://httpbin.org/get?page=1&q=blue+shoes&size=42&size=43&in_stock=true
```

**Python SDK**

```pysdk
from virginproxy import VirginProxy  # docs.virginproxy.com/sdk/virginproxy.py

vp = VirginProxy()  # reads VP_KEY
data = vp.request({
    "url": "https://httpbin.org/get?page=1",
    "params": {"q": "blue shoes", "size": [42, 43], "in_stock": True},
})
print(data)
```

**Node SDK**

```nodesdk
import { VirginProxy } from "./virginproxy.mjs"; // docs.virginproxy.com/sdk/virginproxy.mjs

const vp = new VirginProxy(); // reads VP_KEY
const data = await vp.request({
  url: "https://httpbin.org/get?page=1",
  params: { q: "blue shoes", size: [42, 43], in_stock: true },
});
console.log(data);
```

**PHP**

```php
<?php
$ch = curl_init("https://api.virginproxy.com/v1/requests");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 180,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer " . getenv("VP_KEY"),
        "Content-Type: application/json",
    ],
    CURLOPT_POSTFIELDS => json_encode([
        "url" => "https://httpbin.org/get?page=1",
        "params" => ["q" => "blue shoes", "size" => [42, 43], "in_stock" => true],
    ], JSON_UNESCAPED_SLASHES),
]);
$data = json_decode(curl_exec($ch), true);
if (isset($data["error"])) {
    exit($data["error"]["code"] . ": " . $data["error"]["message"] . PHP_EOL);
}
echo $data["final_url"], PHP_EOL;
```

**Go**

```go
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"
)

func main() {
	payload := []byte(`{
  "url": "https://httpbin.org/get?page=1",
  "params": {
    "q": "blue shoes",
    "size": [
      42,
      43
    ],
    "in_stock": true
  }
}`)
	req, err := http.NewRequest("POST", "https://api.virginproxy.com/v1/requests", bytes.NewReader(payload))
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("Authorization", "Bearer "+os.Getenv("VP_KEY"))
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{Timeout: 180 * time.Second}
	res, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()

	var data map[string]any
	if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
		log.Fatal(err)
	}
	if res.StatusCode >= 300 {
		e := data["error"].(map[string]any)
		log.Fatalf("%v: %v", e["code"], e["message"])
	}
	fmt.Println(data["final_url"])
}
```

POST`/v1/requests`[≈ 1 credit](https://docs.virginproxy.com/billing/#cost)[Open in Playground](https://dash.virginproxy.com/playground#request=eyJ1cmwiOiJodHRwczovL2h0dHBiaW4ub3JnL2dldD9wYWdlPTEiLCJwYXJhbXMiOnsicSI6ImJsdWUgc2hvZXMiLCJzaXplIjpbNDIsNDNdLCJpbl9zdG9jayI6dHJ1ZX19)

Values are strings, numbers, booleans or lists of them, at most 500 values in all. The whole URL may not be longer than 8,000 characters.

## Three kinds of body

| Field | Sent as | Content-Type set for you |
| --- | --- | --- |
| `json` | JSON text of any JSON value | `application/json` |
| `form` | an HTML form: `a=1&b=2` | `application/x-www-form-urlencoded` |
| `body` | exactly the string you give, or bytes with `"body_encoding": "base64"` | none: send your own |

Send at most one of them; together they are refused with `invalid_options`. A `Content-Type` in `headers` always wins. The body may be up to 5 MB.

### JSON

**cURL**

```bash
curl https://api.virginproxy.com/v1/requests \
  -H "Authorization: Bearer $VP_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://httpbin.org/post",
    "method": "POST",
    "json": {
      "query": "shoes",
      "filters": {
        "size": 42,
        "colors": [
          "blue",
          "black"
        ]
      }
    }
  }'
```

**Python**

```python
import os, requests

r = requests.post(
    "https://api.virginproxy.com/v1/requests",
    headers={"Authorization": "Bearer " + os.environ["VP_KEY"]},
    json={
        "url": "https://httpbin.org/post",
        "method": "POST",
        "json": {
            "query": "shoes",
            "filters": {"size": 42, "colors": ["blue", "black"]},
        },
    },
    timeout=180,
)
data = r.json()
if not r.ok:
    raise SystemExit(data["error"]["code"] + ": " + data["error"]["message"])
print(data["body"])
```

**Node.js**

```javascript
const res = await fetch("https://api.virginproxy.com/v1/requests", {
  method: "POST",
  headers: { Authorization: "Bearer " + process.env.VP_KEY, "Content-Type": "application/json" },
  body: JSON.stringify({
    url: "https://httpbin.org/post",
    method: "POST",
    json: {
      query: "shoes",
      filters: { size: 42, colors: ["blue", "black"] },
    },
  }),
  signal: AbortSignal.timeout(180_000),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error.code + ": " + data.error.message);
console.log(data.body);
```

**Python SDK**

```pysdk
from virginproxy import VirginProxy  # docs.virginproxy.com/sdk/virginproxy.py

vp = VirginProxy()  # reads VP_KEY
data = vp.request({
    "url": "https://httpbin.org/post",
    "method": "POST",
    "json": {
        "query": "shoes",
        "filters": {"size": 42, "colors": ["blue", "black"]},
    },
})
print(data)
```

**Node SDK**

```nodesdk
import { VirginProxy } from "./virginproxy.mjs"; // docs.virginproxy.com/sdk/virginproxy.mjs

const vp = new VirginProxy(); // reads VP_KEY
const data = await vp.request({
  url: "https://httpbin.org/post",
  method: "POST",
  json: {
    query: "shoes",
    filters: { size: 42, colors: ["blue", "black"] },
  },
});
console.log(data);
```

**PHP**

```php
<?php
$ch = curl_init("https://api.virginproxy.com/v1/requests");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 180,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer " . getenv("VP_KEY"),
        "Content-Type: application/json",
    ],
    CURLOPT_POSTFIELDS => json_encode([
        "url" => "https://httpbin.org/post",
        "method" => "POST",
        "json" => [
            "query" => "shoes",
            "filters" => ["size" => 42, "colors" => ["blue", "black"]],
        ],
    ], JSON_UNESCAPED_SLASHES),
]);
$data = json_decode(curl_exec($ch), true);
if (isset($data["error"])) {
    exit($data["error"]["code"] . ": " . $data["error"]["message"] . PHP_EOL);
}
echo $data["body"], PHP_EOL;
```

**Go**

```go
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"
)

func main() {
	payload := []byte(`{
  "url": "https://httpbin.org/post",
  "method": "POST",
  "json": {
    "query": "shoes",
    "filters": {
      "size": 42,
      "colors": [
        "blue",
        "black"
      ]
    }
  }
}`)
	req, err := http.NewRequest("POST", "https://api.virginproxy.com/v1/requests", bytes.NewReader(payload))
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("Authorization", "Bearer "+os.Getenv("VP_KEY"))
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{Timeout: 180 * time.Second}
	res, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()

	var data map[string]any
	if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
		log.Fatal(err)
	}
	if res.StatusCode >= 300 {
		e := data["error"].(map[string]any)
		log.Fatalf("%v: %v", e["code"], e["message"])
	}
	fmt.Println(data["body"])
}
```

POST`/v1/requests`[≈ 1 credit](https://docs.virginproxy.com/billing/#cost)[Open in Playground](https://dash.virginproxy.com/playground#request=eyJ1cmwiOiJodHRwczovL2h0dHBiaW4ub3JnL3Bvc3QiLCJtZXRob2QiOiJQT1NUIiwianNvbiI6eyJxdWVyeSI6InNob2VzIiwiZmlsdGVycyI6eyJzaXplIjo0MiwiY29sb3JzIjpbImJsdWUiLCJibGFjayJdfX19)

### Form

Values as in `params`: strings, numbers, booleans or lists of them.

**cURL**

```bash
curl https://api.virginproxy.com/v1/requests \
  -H "Authorization: Bearer $VP_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://httpbin.org/post",
    "method": "POST",
    "form": {
      "email": "ada@example.com",
      "topics": [
        "news",
        "offers"
      ],
      "subscribe": true
    }
  }'
```

**Python**

```python
import os, requests

r = requests.post(
    "https://api.virginproxy.com/v1/requests",
    headers={"Authorization": "Bearer " + os.environ["VP_KEY"]},
    json={
        "url": "https://httpbin.org/post",
        "method": "POST",
        "form": {
            "email": "ada@example.com",
            "topics": ["news", "offers"],
            "subscribe": True,
        },
    },
    timeout=180,
)
data = r.json()
if not r.ok:
    raise SystemExit(data["error"]["code"] + ": " + data["error"]["message"])
print(data["body"])
```

**Node.js**

```javascript
const res = await fetch("https://api.virginproxy.com/v1/requests", {
  method: "POST",
  headers: { Authorization: "Bearer " + process.env.VP_KEY, "Content-Type": "application/json" },
  body: JSON.stringify({
    url: "https://httpbin.org/post",
    method: "POST",
    form: {
      email: "ada@example.com",
      topics: ["news", "offers"],
      subscribe: true,
    },
  }),
  signal: AbortSignal.timeout(180_000),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error.code + ": " + data.error.message);
console.log(data.body);
```

**Python SDK**

```pysdk
from virginproxy import VirginProxy  # docs.virginproxy.com/sdk/virginproxy.py

vp = VirginProxy()  # reads VP_KEY
data = vp.request({
    "url": "https://httpbin.org/post",
    "method": "POST",
    "form": {
        "email": "ada@example.com",
        "topics": ["news", "offers"],
        "subscribe": True,
    },
})
print(data)
```

**Node SDK**

```nodesdk
import { VirginProxy } from "./virginproxy.mjs"; // docs.virginproxy.com/sdk/virginproxy.mjs

const vp = new VirginProxy(); // reads VP_KEY
const data = await vp.request({
  url: "https://httpbin.org/post",
  method: "POST",
  form: {
    email: "ada@example.com",
    topics: ["news", "offers"],
    subscribe: true,
  },
});
console.log(data);
```

**PHP**

```php
<?php
$ch = curl_init("https://api.virginproxy.com/v1/requests");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 180,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer " . getenv("VP_KEY"),
        "Content-Type: application/json",
    ],
    CURLOPT_POSTFIELDS => json_encode([
        "url" => "https://httpbin.org/post",
        "method" => "POST",
        "form" => [
            "email" => "ada@example.com",
            "topics" => ["news", "offers"],
            "subscribe" => true,
        ],
    ], JSON_UNESCAPED_SLASHES),
]);
$data = json_decode(curl_exec($ch), true);
if (isset($data["error"])) {
    exit($data["error"]["code"] . ": " . $data["error"]["message"] . PHP_EOL);
}
echo $data["body"], PHP_EOL;
```

**Go**

```go
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"
)

func main() {
	payload := []byte(`{
  "url": "https://httpbin.org/post",
  "method": "POST",
  "form": {
    "email": "ada@example.com",
    "topics": [
      "news",
      "offers"
    ],
    "subscribe": true
  }
}`)
	req, err := http.NewRequest("POST", "https://api.virginproxy.com/v1/requests", bytes.NewReader(payload))
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("Authorization", "Bearer "+os.Getenv("VP_KEY"))
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{Timeout: 180 * time.Second}
	res, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()

	var data map[string]any
	if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
		log.Fatal(err)
	}
	if res.StatusCode >= 300 {
		e := data["error"].(map[string]any)
		log.Fatalf("%v: %v", e["code"], e["message"])
	}
	fmt.Println(data["body"])
}
```

POST`/v1/requests`[≈ 1 credit](https://docs.virginproxy.com/billing/#cost)[Open in Playground](https://dash.virginproxy.com/playground#request=eyJ1cmwiOiJodHRwczovL2h0dHBiaW4ub3JnL3Bvc3QiLCJtZXRob2QiOiJQT1NUIiwiZm9ybSI6eyJlbWFpbCI6ImFkYUBleGFtcGxlLmNvbSIsInRvcGljcyI6WyJuZXdzIiwib2ZmZXJzIl0sInN1YnNjcmliZSI6dHJ1ZX19)

### Raw text and binary data

`body` is sent unchanged. For binary data, encode it as base64 and add `"body_encoding": "base64"`.

**Python**

```python
import base64, os, requests

with open("photo.jpg", "rb") as f:
    payload = base64.b64encode(f.read()).decode()

r = requests.post(
    "https://api.virginproxy.com/v1/requests",
    headers={"Authorization": "Bearer " + os.environ["VP_KEY"]},
    json={
        "url": "https://httpbin.org/post",
        "method": "POST",
        "headers": {"Content-Type": "image/jpeg"},
        "body": payload,
        "body_encoding": "base64",
    },
    timeout=180,
)
print(r.json()["status"])
```

**Node.js**

```javascript
import { readFile } from "node:fs/promises";

const payload = (await readFile("photo.jpg")).toString("base64");
const res = await fetch("https://api.virginproxy.com/v1/requests", {
  method: "POST",
  headers: { Authorization: "Bearer " + process.env.VP_KEY, "Content-Type": "application/json" },
  body: JSON.stringify({
    url: "https://httpbin.org/post",
    method: "POST",
    headers: { "Content-Type": "image/jpeg" },
    body: payload,
    body_encoding: "base64",
  }),
});
console.log((await res.json()).status);
```
