Adding header for HttpURLConnection
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When you use HttpURLConnection, headers are how you tell the server what you want and who you are. They carry metadata such as authentication tokens, accepted response formats, content types, cache directives, and custom application values.
The important detail is timing. Request headers must be set before the request is sent, which usually means before calling getInputStream(), getOutputStream(), or connect().
Setting Request Headers
The main API is setRequestProperty(key, value). It replaces any previous value for that header name.
This example sets three headers before triggering the network request with getResponseCode().
setRequestProperty Versus addRequestProperty
Use setRequestProperty when a header should have one final value. Use addRequestProperty when a header can appear multiple times or when you intentionally want more than one value.
In practice, setRequestProperty is the common choice. It matches how most application code thinks about headers.
Adding Headers for POST Requests
Headers matter even more when sending a request body. A server often expects a matching Content-Type header so it can parse the body correctly.
If the server expects JSON, sending a body without the correct Content-Type can produce a 400 or 415 response even though the Java code compiled and ran.
Reading Response Headers Too
HttpURLConnection can also read response headers. That is useful for rate-limit tracking, pagination, caching, and redirect logic.
Request headers go from client to server. Response headers come back from server to client. Keeping that direction clear prevents a lot of confusion when debugging.
Common Header Use Cases
Some of the most common request headers are:
- '
Authorizationfor API tokens or basic auth' - '
Acceptfor preferred response format' - '
Content-Typefor request body format' - '
User-Agentfor client identification' - custom
X-...or domain-specific headers for tracing and internal metadata
Use exact header values from the server or API documentation. Header names are standardized in many cases, but the required values are application-specific.
Common Pitfalls
- Setting headers after the request has already started. Once the connection is established, later header changes may be ignored.
- Forgetting
setDoOutput(true)before writing a request body onPOSTorPUTrequests. - Mixing request headers and response headers conceptually.
getHeaderFieldreads the response; it does not set outgoing values. - Using the wrong
Content-Type, which causes the server to reject or misinterpret the body. - Ignoring timeouts and error streams, making header-related failures look like hangs or empty responses.
Summary
- Use
setRequestPropertyto add or replace an outgoing HTTP header. - Set headers before sending the request.
- For body-based requests, make
Content-Typematch the payload format. - '
addRequestPropertyis only needed when multiple values are intentional.' - Good header handling usually depends more on order and correctness than on API complexity.

