Skip to content

Instantly share code, notes, and snippets.

@mjg123
Last active August 19, 2020 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjg123/cb5ee1be4c2992df47cf67d9b76b3062 to your computer and use it in GitHub Desktop.
Save mjg123/cb5ee1be4c2992df47cf67d9b76b3062 to your computer and use it in GitHub Desktop.
// This method exists to work around a quirk in HttpServletRequest. For validation we only
// need to consider the parameters from the request *body*, but HSR.getParameterMap() *also* includes
// any parameters found in the query String, which will cause validation to fail.
// For POST requests, Twilio will not add any queryString params, but it's perfectly possible for a
// user to do that by including them in the webhook URL.
private HashMap<String, String> extractOnlyBodyParams(HttpServletRequest request, String validationUrl) throws IOException {
var allRequestParameters = request.getParameterMap();
var queryStringParams = UriComponentsBuilder.fromUriString(validationUrl).build().getQueryParams();
var validationParams = new HashMap<String, String>();
// loop through _all_ parameters, only keeping ones which _don't_ appear in the query string.
allRequestParameters.forEach((name, values) -> {
for (String value : values) {
if (!(queryStringParams.containsKey(name) && queryStringParams.get(name).contains(value))) {
validationParams.put(name, value);
}
}
});
return validationParams;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment