Send a binary HTTP request body: preserve bytes and set Content-Type
When an API accepts a file or binary payload, send the original bytes as the request body and use the media type required by the endpoint. application/octet-stream is a general choice for arbitrary bytes; when the format is known, such as JPEG or PDF, use its specific media type.
When an API accepts a file or binary payload, send the original bytes as the request body and use the media type required by the endpoint. application/octet-stream is a general choice for arbitrary bytes; when the format is known, such as JPEG or PDF, use its specific media type.
An HTTP body is not limited to text. The Content-Type header describes the representation’s format; it does not describe the body length or message boundary. For a raw JPEG endpoint, the request may look like this:
POST /v1/images HTTP/1.1
Content-Type: image/jpeg
<JPEG bytes>The browser Fetch API accepts File, Blob, ArrayBuffer, TypedArray and other body types. For a File, send the object directly:
async function uploadImage(file) {
const response = await fetch("/v1/images", {
method: "POST",
headers: { "Content-Type": file.type || "application/octet-stream" },
body: file
});
if (!response.ok) throw new Error("Upload failed: HTTP " + response.status);
return response.json();
}This example assumes the endpoint returns JSON; follow the actual API contract. For a byte array, pass the bytes as body and describe their actual format. In browsers, do not set Content-Length manually; the browser manages this forbidden request header. Use FormData only when the API expects multipart fields and files. If multipart is required, let the browser generate the boundary.
Keep payload type separate from framing. Content-Length can state the body size in octets for HTTP/1.1; transfer coding handles transmission and message boundaries. Usually, provide the bytes and Content-Type and let the HTTP library manage framing. Do not set Content-Length and Transfer-Encoding together arbitrarily.
Check server limits, timeouts, storage and authorization before sending large bodies. Cross-origin browser requests with a non-safelisted Content-Type such as application/octet-stream may trigger a CORS preflight; the server must permit the origin and headers. On receipt, allow-list media types, safely inspect actual content and enforce size limits. A filename or client-supplied type does not prove the content’s format.
| API expects | Body | Example Content-Type |
|---|---|---|
| Raw JPEG bytes | File or byte array | image/jpeg |
| Arbitrary bytes | File or byte array | application/octet-stream |
| File plus form fields | FormData | Browser generates multipart header |
Send unchanged bytes, accurately describe the payload and follow the endpoint’s contract for framing, validation and response handling.
Fetch example
async function uploadImage(file) {
const response = await fetch("/v1/images", {
method: "POST",
headers: { "Content-Type": file.type || "application/octet-stream" },
body: file
});
if (!response.ok) throw new Error("Upload failed: HTTP " + response.status);
return response.json();
}Sources
[RFC 9110 §8.3 Content-Type](https://www.rfc-editor.org/rfc/rfc9110.html#section-8.3)
[RFC 9112 §6 Message body and length](https://www.rfc-editor.org/rfc/rfc9112.html#section-6)
[MDN RequestInit: body](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#body)
[MDN Fetch API: Setting a body](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_a_body)