@Test public void getRequestData() throws Exception { String requestJson = "{\n" + " \"method\": \"POST\",\n" + " \"url\": \"https: " \"httpVersion\": \"HTTP/1.1\",\n" + " \"cookies\": [],\n" + " \"headers\": [\n" + " {\n" + " \"name\": \"Host\",\n" + " \"value\": \"www.example.com\"\n" + " },\n" + " {\n" + " \"name\": \"Connection\",\n" + " \"value\": \"keep-alive\"\n" + " },\n" + " {\n" + " \"name\": \"Content-Length\",\n" + " \"value\": \"116\"\n" + " },\n" + " {\n" + " \"name\": \"X-ABCDEF-Protocol-Version\",\n" + " \"value\": \"Foo\"\n" + " },\n" + " {\n" + " \"name\": \"User-Agent\",\n" + " \"value\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0 Safari/537.36\"\n" + " },\n" + " {\n" + " \"name\": \"Content-Type\",\n" + " \"value\": \"multipart/mixed; boundary\\u003dABCDEF_1522096137171\"\n" + " },\n" + " {\n" + " \"name\": \"Accept\",\n" + " \"value\": \"application/json, text/javascript, */*; q\\u003d0.01\"\n" + " },\n" + " {\n" + " \"name\": \"X-Requested-With\",\n" + " \"value\": \"XMLHttpRequest\"\n" + " },\n" + " {\n" + " \"name\": \"Accept-Encoding\",\n" + " \"value\": \"gzip, deflate, br\"\n" + " },\n" + " {\n" + " \"name\": \"Accept-Language\",\n" + " \"value\": \"en-US,en;q\\u003d0.9\"\n" + " }\n" + " ],\n" + " \"queryString\": [],\n" + " \"postData\": {\n" + " \"mimeType\": \"multipart/mixed; boundary\\u003dABCDEF_1522096137171\",\n" + " \"params\": [],\n" + " \"text\": \"--ABCDEF_1522096137171\\r\\nContent-Type: application/x-www-form-urlencoded\\r\\n\\r\\nfoo\\u003dbar\\u0026baz\\u003dgaw\\r\\n--ABCDEF_1522096137171--\",\n" + " \"comment\": \"\"\n" + " },\n" + " \"headersSize\": 1891,\n" + " \"bodySize\": 116,\n" + " \"comment\": \"\"\n" + "}"; SstoehrHarBridge bridge = new SstoehrHarBridge(); HarRequest request = new Gson().fromJson(requestJson, HarRequest.class); HarEntry entry = new HarEntry(); entry.setRequest(request); ByteSource byteSource = bridge.getRequestPostData(entry); assertNotNull("post data byte source", byteSource); byte[] expected = request.getPostData().getText().getBytes(StandardCharsets.US_ASCII); assertEquals("post data bytes", BaseEncoding.base16().encode(expected), BaseEncoding.base16().encode(byteSource.read())); }
@Nullable @Override public ByteSource getRequestPostData(HarEntry entry) throws IOException { HarRequest request = entry.getRequest(); if (request != null) { HarPostData postData = request.getPostData(); if (postData != null) { List<HarPostDataParam> params = postData.getParams(); List<NameValuePair> pairs = null; if (params != null) { pairs = params.stream().map(p -> NameValuePair.of(p.getName(), p.getValue())).collect(Collectors.toList()); } String contentType = postData.getMimeType(); String postDataText = postData.getText(); @Nullable Long requestBodySize = nullIfNegative(request.getBodySize()); @Nullable String postDataComment = postData.getComment(); return Hars.getRequestPostData(pairs, contentType, postDataText, requestBodySize, postDataComment, exMachinaCharset); } } return null; }
SstoehrHarBridge implements HarBridge<HarEntry> { @Nullable @Override public ByteSource getRequestPostData(HarEntry entry) throws IOException { HarRequest request = entry.getRequest(); if (request != null) { HarPostData postData = request.getPostData(); if (postData != null) { List<HarPostDataParam> params = postData.getParams(); List<NameValuePair> pairs = null; if (params != null) { pairs = params.stream().map(p -> NameValuePair.of(p.getName(), p.getValue())).collect(Collectors.toList()); } String contentType = postData.getMimeType(); String postDataText = postData.getText(); @Nullable Long requestBodySize = nullIfNegative(request.getBodySize()); @Nullable String postDataComment = postData.getComment(); return Hars.getRequestPostData(pairs, contentType, postDataText, requestBodySize, postDataComment, exMachinaCharset); } } return null; } }
SstoehrHarBridge implements HarBridge<HarEntry> { @Nullable @Override public ByteSource getRequestPostData(HarEntry entry) throws IOException { HarRequest request = entry.getRequest(); if (request != null) { HarPostData postData = request.getPostData(); if (postData != null) { List<HarPostDataParam> params = postData.getParams(); List<NameValuePair> pairs = null; if (params != null) { pairs = params.stream().map(p -> NameValuePair.of(p.getName(), p.getValue())).collect(Collectors.toList()); } String contentType = postData.getMimeType(); String postDataText = postData.getText(); @Nullable Long requestBodySize = nullIfNegative(request.getBodySize()); @Nullable String postDataComment = postData.getComment(); return Hars.getRequestPostData(pairs, contentType, postDataText, requestBodySize, postDataComment, exMachinaCharset); } } return null; } SstoehrHarBridge();  SstoehrHarBridge(Charset exMachinaCharset); }
SstoehrHarBridge implements HarBridge<HarEntry> { @Nullable @Override public ByteSource getRequestPostData(HarEntry entry) throws IOException { HarRequest request = entry.getRequest(); if (request != null) { HarPostData postData = request.getPostData(); if (postData != null) { List<HarPostDataParam> params = postData.getParams(); List<NameValuePair> pairs = null; if (params != null) { pairs = params.stream().map(p -> NameValuePair.of(p.getName(), p.getValue())).collect(Collectors.toList()); } String contentType = postData.getMimeType(); String postDataText = postData.getText(); @Nullable Long requestBodySize = nullIfNegative(request.getBodySize()); @Nullable String postDataComment = postData.getComment(); return Hars.getRequestPostData(pairs, contentType, postDataText, requestBodySize, postDataComment, exMachinaCharset); } } return null; } SstoehrHarBridge();  SstoehrHarBridge(Charset exMachinaCharset); @Override String getRequestMethod(HarEntry entry); @Override String getRequestUrl(HarEntry entry); @Override Stream<Map.Entry<String, String>> getRequestHeaders(HarEntry entry); @Nullable @Override ByteSource getRequestPostData(HarEntry entry); @Override HarResponseData getResponseData(ParsedRequest request, HarEntry entry, HarResponseEncoding encoding); @Override int getResponseStatus(HarEntry entry); }
SstoehrHarBridge implements HarBridge<HarEntry> { @Nullable @Override public ByteSource getRequestPostData(HarEntry entry) throws IOException { HarRequest request = entry.getRequest(); if (request != null) { HarPostData postData = request.getPostData(); if (postData != null) { List<HarPostDataParam> params = postData.getParams(); List<NameValuePair> pairs = null; if (params != null) { pairs = params.stream().map(p -> NameValuePair.of(p.getName(), p.getValue())).collect(Collectors.toList()); } String contentType = postData.getMimeType(); String postDataText = postData.getText(); @Nullable Long requestBodySize = nullIfNegative(request.getBodySize()); @Nullable String postDataComment = postData.getComment(); return Hars.getRequestPostData(pairs, contentType, postDataText, requestBodySize, postDataComment, exMachinaCharset); } } return null; } SstoehrHarBridge();  SstoehrHarBridge(Charset exMachinaCharset); @Override String getRequestMethod(HarEntry entry); @Override String getRequestUrl(HarEntry entry); @Override Stream<Map.Entry<String, String>> getRequestHeaders(HarEntry entry); @Nullable @Override ByteSource getRequestPostData(HarEntry entry); @Override HarResponseData getResponseData(ParsedRequest request, HarEntry entry, HarResponseEncoding encoding); @Override int getResponseStatus(HarEntry entry); static final Charset DEFAULT_EX_MACHINA_CHARSET; }