Most developers who are working on Light are building the final business handlers. Their job is to get the HTTP query parameters, path parameters and request body in the generated request handlers and create the corresponding response based on the input above. When users choose different HTTP frameworks, they need to know how to get the details from the request and then send the response. This document will be focusing on the request details.
To read the header, you can use the following method.
The Headers class defines a lot of HttpString objects for standard headers. If you cannot find yours, you can create a constant to define yours. It is faster than creating HttpString every time it is used.
public static final HttpString BASIC = new HttpString("Basic");
List<Map<String, Object>> body = (List)exchange.getAttachment(BodyHandler.REQUEST_BODY);
This is the normal web form submission, and the result will be converted to a map or list if the body handler is configured in the chain just like the JSON above.
In case you don’t have BodyHandler in the chain for the endpoint, you can use the following code to parse the form data manually.
// get the form from the exchange
final FormData data = exchange.getAttachment(FormDataParser.FORM_DATA);
final FormData.FormValue jClientId = data.getFirst("client_id");
final FormData.FormValue jRedirectUri = data.getFirst("redirect_uri");
final FormData.FormValue jState = data.getFirst("state");
final FormData.FormValue jRemember = data.getFirst("remember");
final String clientId = jClientId.getValue();
final String remember = jRemember == null ? null : jRemember.getValue(); // should be 'Y' or 'N' if not null.
String redirectUri = jRedirectUri == null ? null : jRedirectUri.getValue();
final String state = jState == null ? null : jState.getValue();
logger.debug("client_id = " + clientId + " state = " + state + " redirectUri = " + redirectUri + " remember = " + remember);
For most users, we recommend to add the BodyHandler to the endpoint in the handler.yml and get the map from the exchange attachment.
For users who want to send binary data from a client to a server, you can use this media type. The BodyHander will convert this to a stream and put it into the exchange as an attachment. Here is the code in the BodyHandler.