Title: Handle non-root path requests · Issue #232 · gaul/java-httpbin · GitHub
Open Graph Title: Handle non-root path requests · Issue #232 · gaul/java-httpbin
X Title: Handle non-root path requests · Issue #232 · gaul/java-httpbin
Description: Hello, For my use case, I would need java-httpbin to support handling requests on non-root paths, not only on the root path. For example, for the /headers endpoint, we are currently forced to call http://127.0.0.1:8080/headers, whereas i...
Open Graph Description: Hello, For my use case, I would need java-httpbin to support handling requests on non-root paths, not only on the root path. For example, for the /headers endpoint, we are currently forced to call ...
X Description: Hello, For my use case, I would need java-httpbin to support handling requests on non-root paths, not only on the root path. For example, for the /headers endpoint, we are currently forced to call ...
Opengraph URL: https://github.com/gaul/java-httpbin/issues/232
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Handle non-root path requests","articleBody":"Hello,\n\nFor my use case, I would need java-httpbin to support handling requests on non-root paths, not only on the root path.\n\nFor example, for the `/headers` endpoint, we are currently forced to call `http://127.0.0.1:8080/headers`, whereas in my case I would need to call `http://127.0.0.1:8080/some/other/path/headers` (which returns `501 Not Implemented`).\n\nI thought of three possible options. Please let me know your thoughts, or if there is another option I may have missed.\n\nThank you!\n\n---\n\n### Option 1: Custom `HttpBinHandler`\n\nAs mentioned in #15, I could indeed implement my own `HttpBinHandler`, but it would mostly be copy-and-paste work.\n\nSince I would need to keep my implementation in sync with future changes in java-httpbin, this solution is neither ideal nor future-proof.\n\n---\n\n### Option 2: Modify `HttpBinHandler` to support non-root paths\n\nModify `HttpBinHandler` so that it also handles non-root paths by default.\n\nNot sure it is a valid approach as it seems to be a breaking change.\n\n```java\n// HttpBinHandler\nif (method.equals(\"GET\") && uri.equals(\"/headers\")) // Current implementation\nif (method.equals(\"GET\") && uri.endsWith(\"/headers\")) // Proposed implementation: \"endsWith\" instead of \"equals\"\n```\n\n---\n\n### Option 3: Refactor `HttpBinHandler` to make it easier to extend/override\n\nInstead of having a large `if / else if / else` block in a single method, perhaps a `List\u003cHttpBinRoute>` could be introduced and iterated until a matching route is found.\n\nThis would make it easy to:\n- Extend existing routes to adjust matching logic or behavior\n- Add custom routes and behavior that are not provided by java-httpbin\n\nIt requires a bit more work, but it is also the most flexible approach. I would be glad to help 😊.\n\n```java\n/** \n * HttpBinHandler\n */\n\nprivate List\u003cHttpBinRoute> customRoutes = new ArrayList\u003c>(); // mutable with some addCustomRoute(HttpBinRoute) method\nprivate List\u003cHttpBinRoute> defaultRoutes = /* default routes */;\n\nprivate void handleHelper(Request baseRequest, HttpServletRequest request,\n HttpServletResponse servletResponse, InputStream is,\n OutputStream os) throws IOException {\n if(!handleRoutes(customRoutes, baseRequest, request, servletResponse, is, os)) { // Try first to handle request with custom routes...\n handleRoutes(defaultRoutes , baseRequest, request, servletResponse, is, os); // ... fallback to default routes otherwise\n }\n}\n\nprivate boolean handleRoutes(List\u003cHttpBinRoute> routes, Request baseRequest, HttpServletRequest request,\n HttpServletResponse servletResponse, InputStream is,\n OutputStream os) throws IOException {\n // try\n for(var route : routes) {\n if(route.matches(request)) {\n route.handle(baseRequest, request, servletResponse, is, os);\n return true;\n }\n }\n // catch JSONException\n return false;\n}\n\n// ------------------------------------------------------------------------------\n\n/** \n * New interface\n */\npublic interface HttpBinRoute {\n\n boolean matches(Request request);\n\n void handle(Request baseRequest, HttpServletRequest request, \n HttpServletResponse servletResponse, InputStream is,\n OutputStream os) throws IOException;\n}\n\n/** \n * Default implementation from java-httpbin.\n * One implementation for each route handled by java-httpbin: HeadersRoute, GetRoute, PostRoute, IpRoute, etc.\n */\npublic class HeadersRoute implements HttpBinRoute {\n\n public boolean matches(Request request) {\n return request.getMethod().equals(\"GET\") && request.getRequestURI().equals(\"/headers\");\n }\n\n public void handle(...) throw IOException, JSONException {\n // default implementation from HttpBinHandler\n }\n}\n\n// ------------------------------------------------------------------------------\n\n/**\n * Custom implementation that only overrides the \"matches\" method.\n */\npublic class MyCustomHeadersRoute extends HeadersRoute {\n\n @Override\n public boolean matches(Request request) {\n return request.getMethod().equals(\"GET\") && request.getRequestURI().endsWith(\"/headers\");\n }\n\n}\n\nvar handler = new HttpBinHandler();\nhandler.addCustomRoute(new MyCustomHeadersRoute());\nvar httpBin = new HttpBin( uri, handler );\nhttpBin.start();\n```\n","author":{"url":"https://github.com/ComputerDaddyGuy","@type":"Person","name":"ComputerDaddyGuy"},"datePublished":"2026-08-28T06:29:24Z","url":"https://github.com/gaul/java-httpbin/issues/232"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:025fbdf3-af1d-b9a8-a10e-dddb83b0aaa1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B728:2F672C:F25EA:1285DD:6B0A05B0 |
| html-safe-nonce | 39081879ed43d8ccd441e6c8e8429b7ff2818a42579483cb3ec54bde30bf612c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNzI4OjJGNjcyQzpGMjVFQToxMjg1REQ6NkIwQTA1QjAiLCJ2aXNpdG9yX2lkIjoiMTUxMzUxMTE3MjI3NTA0NTgwOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 69eff03391494ab254ccc9ef7426cb9db21d22c2b0c0bdb840cadba2494a5159 |
| hovercard-subject-tag | issue:5275370636 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/gaul/java-httpbin/232/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a28eb3b75169f1666f19c3c333ec1d1bd8fc45426494f962d4c6f95f8c8a48bc/gaul/java-httpbin/issues/232 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a28eb3b75169f1666f19c3c333ec1d1bd8fc45426494f962d4c6f95f8c8a48bc/gaul/java-httpbin/issues/232 |
| og:image:alt | Hello, For my use case, I would need java-httpbin to support handling requests on non-root paths, not only on the root path. For example, for the /headers endpoint, we are currently forced to call ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ComputerDaddyGuy |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7c47183a73008933468557b28d2500e2ea7619094b9fe68a81910d76811eeb3c |
| turbo-cache-control | no-preview |
| go-import | github.com/gaul/java-httpbin git https://github.com/gaul/java-httpbin.git |
| octolytics-dimension-user_id | 848247 |
| octolytics-dimension-user_login | gaul |
| octolytics-dimension-repository_id | 153568923 |
| octolytics-dimension-repository_nwo | gaul/java-httpbin |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 153568923 |
| octolytics-dimension-repository_network_root_nwo | gaul/java-httpbin |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 206ca5e2e4c7feca284ad1771fa08530fd32785c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width