class HttpRequestMsg {

    public HttpRequest createHttpRequest(HttpRequestFactory requestFactory, String baseUrl) throws IOException {
        HttpContent requestContent = new EmptyContent();
        if (payload != null) {
            ByteArrayInputStream payloadStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
            requestContent = new InputStreamContent("application/json", payloadStream);
        }

        HttpHeaders httpHeaders = new HttpHeaders();
        for (Map.Entry<String, String> header : headers.entrySet()) {
            String headerName = header.getKey();
            Object headerValue = header.getValue();

            /*
             * Google's HTTP client doesn't allow you to use set(String, String) on certain headers. The FieldInfo
             * checks this and creates a singleton list on these headers.
             */
            FieldInfo fieldInfo = httpHeaders.getClassInfo().getFieldInfo(headerName);
            if (fieldInfo != null) {
                headerValue = Collections.singletonList(headerValue);
            }
            httpHeaders.set(headerName, headerValue);
        }

        GenericUrl fullUrl = new GenericUrl(baseUrl + "/" + this.url);
        HttpRequest request = requestFactory.buildRequest(verb, fullUrl, requestContent);
        request.setHeaders(httpHeaders);
        request.setThrowExceptionOnExecuteError(false); // allows us to check for errors (e.g. 404, 500)

        return request;
    }

}