public static HttpReq body(byte[] body) { return req().body(body); }
private static NByteArrayEntity byteBody(HttpReq config) { NByteArrayEntity entity = new NByteArrayEntity(config.body()); if (config.contentType() != null) { entity.setContentType(config.contentType()); } return entity; }
static HttpRequestBase createRequest(HttpReq config) { Map<String, String> headers = U.safe(config.headers()); Map<String, String> cookies = U.safe(config.cookies()); String url = config.url(); url = Msc.urlWithProtocol(url); HttpRequestBase req = createReq(config, url); for (Map.Entry<String, String> e : headers.entrySet()) { req.addHeader(e.getKey(), e.getValue()); } if (U.notEmpty(cookies)) { req.addHeader("Cookie", joinCookiesAsHeader(cookies)); } switch (config.verb()) { case POST: case PUT: case PATCH: HttpEntityEnclosingRequestBase entityEnclosingReq = (HttpEntityEnclosingRequestBase) req; if (config.body() != null) { entityEnclosingReq.setEntity(byteBody(config)); } else if (U.notEmpty(config.data()) || U.notEmpty(config.files())) { entityEnclosingReq.setEntity(paramsBody(config.data(), config.files())); } break; } req.setConfig(reqConfig(config)); return req; }
private void process(final Req req, final Resp resp, final ProxyMapping mapping, final int attempts, final long since) { final String targetUrl = mapping.getTargetUrl(req); Map<String, String> headers = U.map(req.headers()); headers.remove("transfer-encoding"); headers.remove("content-length"); addExtraRequestHeaders(req, headers); HttpClient client = getOrCreateClient(); client.req() .verb(req.verb()) .url(targetUrl) .headers(headers) .cookies(req.cookies()) .body(req.body()) .raw(true) .execute((result, error) -> { if (error == null) { resp.code(result.code()); resp.body(result.bodyBytes()); // process the response headers SimpleHttpResp proxyResp = new SimpleHttpResp(); HttpUtils.proxyResponseHeaders(result.headers(), proxyResp); if (proxyResp.contentType != null) resp.contentType(proxyResp.contentType); if (proxyResp.headers != null) resp.headers().putAll(proxyResp.headers); if (proxyResp.cookies != null) resp.cookies().putAll(proxyResp.cookies); resp.done(); } else { handleError(error, req, resp, mapping, attempts, since); } }); }
.headers(headers) .cookies(req.cookies()) .body(req.body()) .raw(true) .execute(new Callback<HttpResp>() {