How to set a request header with Java HttpURLConnection
Codemia Knowledge Hub ·
Call setRequestProperty(name, value) on the HttpURLConnection before the request is sent, which means before connect(), getInputStream(), getOutputStream() or getResponseCode(). It replaces any previous value for that header name. Headers carry metadata such as authentication tokens, accepted response formats, content types, cache directives and custom application values.
Setting request headers
All three headers are set before getResponseCode() triggers the network request.
setRequestProperty versus addRequestProperty
Use setRequestProperty when a header should have one final value. Use addRequestProperty when a header can appear more than once or you intentionally want several values.
setRequestProperty is the usual choice, since most application code treats a header as having one value.
Headers for POST requests
When you send a body, the server usually needs a matching Content-Type header to parse it:
If the server expects JSON and the Content-Type is wrong or missing, you can get a 400 or 415 response even though the Java code compiled and ran.
Reading response headers
getHeaderField reads headers the server sent back, which is useful for rate limits, pagination, caching and redirects:
Request headers go from client to server; response headers come back from server to client. getHeaderField does not set outgoing values.
Commonly set request headers
Authorizationfor API tokens or basic authAcceptfor the preferred response formatContent-Typefor the request body formatUser-Agentfor client identification- custom
X-...or domain-specific headers for tracing and internal metadata
Header names are often standardized, but the required values depend on the API, so take them from its documentation.
Why a header seems to be ignored
- It was set after the request started. Once the connection is established,
setRequestPropertythrowsjava.lang.IllegalStateException: Already connectedinstead of changing the request. setDoOutput(true)was not called before writing aPOSTorPUTbody. This is not a silent failure:getOutputStream()throwsjava.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true).- The
Content-Typedoes not match the body, so the server rejects or misreads it. - Missing timeouts and unread error streams make header problems look like hangs or empty responses.
Related reading
- Adding header to all request with Retrofit 2
- Adding Headers to Zuul when re-directing
- Adding n hours to a date in Java?
- Adding Tensorboard summaries from graph ops generated inside Dataset map function calls
- Adding Lombok plugin to IntelliJ project
- adding multiple entries to a HashMap at once in one statement
- AFNetworking 2.0 add headers to GET request
- AFNetworking Post Request
System design interview problems on this topic
- Design a Load Balancer: solution and walkthrough
- Design a Domain Name System: solution and walkthrough
- Design a Global Content Distribution Network: solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.