Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation for Custom http status code #11445

Open
wants to merge 3 commits into
base: 4.7.x
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/main/docs/guide/httpServer/customHttpStatusCode
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
The Micronaut framework supports Custom HTTP Status Codes . This feature enables developers to define and use HTTP status codes beyond the standard ones, allowing integration with systems or APIs that rely on non-standard status codes.
Nitishrajj marked this conversation as resolved.
Show resolved Hide resolved


=== Custom HTTP Status Codes
Nitishrajj marked this conversation as resolved.
Show resolved Hide resolved
Micronaut now supports arbitrary HTTP status codes that are not part of the predefined `HttpStatus` enum. Developers can specify any valid HTTP status code within the range `100-599`.
Nitishrajj marked this conversation as resolved.
Show resolved Hide resolved

==== Usage
- `HttpResponse.status(int statusCode)`:
Allows setting a custom HTTP status code.
- `HttpResponse.status(int statusCode, String reason)`:
Allows setting a custom HTTP status code along with a reason phrase.

==== Examples
[source,java]
----
HttpResponse.status(450, "Blocked by ISP");
HttpResponse.status(599, "Network Connect Timeout Error");
----
Nitishrajj marked this conversation as resolved.
Show resolved Hide resolved

=== Validation of Status Codes
Micronaut ensures that the custom status codes are within the valid HTTP range (`100-599`).

- Valid Codes: Between `100` and `599`.
- Invalid Codes: Codes outside this range (e.g., `700` or negative values) will throw an `IllegalArgumentException`.

==== Examples
[source,java]
----
HttpResponse.status(700); // Throws IllegalArgumentException
HttpResponse.status(-1); // Throws IllegalArgumentException
----

=== Client Response Handling
The HTTP client in Micronaut can now process custom status codes returned by servers. Custom codes are treated appropriately based on their range:
- Codes `<400` are treated as successful responses.
- Codes `>=400` trigger exceptions.

==== Example
[source,java]
----
try {
HttpResponse response = httpClient.toBlocking().exchange("/example");
int statusCode = response.code();
} catch (HttpClientResponseException e) {
if (e.getStatus().getCode() == 450) {
yawkat marked this conversation as resolved.
Show resolved Hide resolved
// Handle custom status code
}
}
----

== Backward Compatibility
This feature is fully backward-compatible:
- Existing applications using predefined HTTP status codes remain unaffected.
- Custom status codes are additive, enabling new use cases without breaking existing behavior.

== Summary
The addition of custom HTTP status code support in Micronaut enhances its flexibility, enabling developers to integrate with systems using non-standard codes. With this feature, arbitrary HTTP status codes can be used while maintaining full compatibility with existing functionality.
Nitishrajj marked this conversation as resolved.
Show resolved Hide resolved