/** * Configures the headers in the specified Apache HTTP request. */ private void addHeadersToRequest(HttpRequestBase httpRequest, Request<?> request) { httpRequest.addHeader(HttpHeaders.HOST, getHostHeaderValue(request.getEndpoint())); // Copy over any other headers already in our request for (Entry<String, String> entry : request.getHeaders().entrySet()) { /* * HttpClient4 fills in the Content-Length header and complains if * it's already present, so we skip it here. We also skip the Host * header to avoid sending it twice, which will interfere with some * signing schemes. */ if (!(ignoreHeaders.contains(entry.getKey()))) { httpRequest.addHeader(entry.getKey(), entry.getValue()); } } /* Set content type and encoding */ if (httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE) == null || httpRequest .getHeaders (HttpHeaders.CONTENT_TYPE).length == 0) { httpRequest.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded; " + "charset=" + DEFAULT_ENCODING.toLowerCase()); } }
HttpEntity entity = request.getBody().getEntity(); if (async) { if (reqObj.getHeaders(CONTENT_TYPE) == null || reqObj.getHeaders(CONTENT_TYPE).length == 0) { reqObj.setHeader(entity.getContentType());
/** * Configures the headers in the specified Apache HTTP request. */ private void addHeadersToRequest(HttpRequestBase httpRequest, Request<?> request) { httpRequest.addHeader(HttpHeaders.HOST, getHostHeaderValue(request.getEndpoint())); // Copy over any other headers already in our request for (Entry<String, String> entry : request.getHeaders().entrySet()) { /* * HttpClient4 fills in the Content-Length header and complains if * it's already present, so we skip it here. We also skip the Host * header to avoid sending it twice, which will interfere with some * signing schemes. */ if (!(ignoreHeaders.contains(entry.getKey()))) { httpRequest.addHeader(entry.getKey(), entry.getValue()); } } /* Set content type and encoding */ if (httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE) == null || httpRequest .getHeaders (HttpHeaders.CONTENT_TYPE).length == 0) { httpRequest.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded; " + "charset=" + DEFAULT_ENCODING.toLowerCase()); } }
@Override public void interceptRequest(IHttpRequest theRequestInterface) { HttpRequestBase theRequest = ((ApacheHttpRequest) theRequestInterface).getApacheRequest(); if (theRequest instanceof HttpEntityEnclosingRequest) { Header[] encodingHeaders = theRequest.getHeaders(Constants.HEADER_CONTENT_ENCODING); if (encodingHeaders == null || encodingHeaders.length == 0) { HttpEntityEnclosingRequest req = (HttpEntityEnclosingRequest)theRequest; ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream gos; try { gos = new GZIPOutputStream(bos); req.getEntity().writeTo(gos); gos.finish(); } catch (IOException e) { ourLog.warn("Failed to GZip outgoing content", e); return; } byte[] byteArray = bos.toByteArray(); ByteArrayEntity newEntity = new ByteArrayEntity(byteArray); req.setEntity(newEntity); req.addHeader(Constants.HEADER_CONTENT_ENCODING, "gzip"); } } }
private void configureHeaders(HttpRequestBase httpRequest, Map<String, String> headers) { for (Map.Entry<String, String> entry : headers.entrySet()) { if ((entry.getKey()).equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)) continue; if ((entry.getKey()).equalsIgnoreCase(HttpHeaders.HOST)) continue; httpRequest.addHeader(entry.getKey(), entry.getValue()); } if (httpRequest instanceof HttpEntityEnclosingRequestBase) { HttpEntityEnclosingRequestBase entityEnclosingRequest = (HttpEntityEnclosingRequestBase) httpRequest; if (entityEnclosingRequest.getEntity().getContentType() == null && (httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE) == null || httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE).length == 0)) { httpRequest.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=UTF-8".toLowerCase()); } } else if (httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE) == null || httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE).length == 0) { httpRequest.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=UTF-8".toLowerCase()); } }
/** * Returns all headers with given name. * @param name header name * @return All registered headers with given name. If there are no such headers, an empty set is returned * @throws IllegalArgumentException if name is null or empty */ public Set<HttpHeader> getHeaders(String name) throws IllegalArgumentException { if (name == null || name.isEmpty()) throw new IllegalArgumentException("Null/Empty name"); Set<HttpHeader> headerSet = new LinkedHashSet<>(); Header[] headers = getCoreRequest().getHeaders(name); for (Header header : headers) { headerSet.add(new HttpHeader(header.getName(), header.getValue())); } return Collections.unmodifiableSet(headerSet); }
private void configureRequestHeaders(ServiceClient.Request request, String charset, HttpRequestBase httpRequest){ // Copy headers in the request message to the HTTP request for(Entry<String, String> entry : request.getHeaders().entrySet()){ // HttpClient fills in the Content-Length, // and complains if add it again, so skip it as well as the Host header. /* if (entry.getKey().equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH) || entry.getKey().equalsIgnoreCase(HttpHeaders.HOST)){ continue; } */ if (entry.getKey().equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)){ continue; } httpRequest.addHeader(entry.getKey(), entry.getValue()); } // Set content type and encoding if (httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE) == null || httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE).length == 0){ httpRequest.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded; " + "charset=" + charset.toLowerCase()); } }
private void configureRequestHeaders(ServiceClient.Request request, String charset, HttpRequestBase httpRequest){ // Copy headers in the request message to the HTTP request for(Entry<String, String> entry : request.getHeaders().entrySet()){ // HttpClient fills in the Content-Length, // and complains if add it again, so skip it as well as the Host header. /* if (entry.getKey().equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH) || entry.getKey().equalsIgnoreCase(HttpHeaders.HOST)){ continue; } */ if (entry.getKey().equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)){ continue; } httpRequest.addHeader(entry.getKey(), entry.getValue()); } // Set content type and encoding if (httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE) == null || httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE).length == 0){ httpRequest.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded; " + "charset=" + charset.toLowerCase()); } }
/** * Configures the headers in the specified Apache HTTP request. */ private void addHeadersToRequest(HttpRequestBase httpRequest, SdkHttpRequest request) { httpRequest.addHeader(HttpHeaders.HOST, getHostHeaderValue(request)); // Copy over any other headers already in our request request.headers().entrySet().stream() /* * HttpClient4 fills in the Content-Length header and complains if * it's already present, so we skip it here. We also skip the Host * header to avoid sending it twice, which will interfere with some * signing schemes. */ .filter(e -> !IGNORE_HEADERS.contains(e.getKey())) .forEach(e -> e.getValue().forEach(h -> httpRequest.addHeader(e.getKey(), h))); /* Set content type and encoding */ if (httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE) == null || httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE).length == 0) { httpRequest.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded; " + "charset=" + lowerCase(DEFAULT_ENCODING)); } }
/** * Configures the headers in the specified Apache HTTP request. */ private void addHeadersToRequest(HttpRequestBase httpRequest, SdkHttpFullRequest request) { httpRequest.addHeader(HttpHeaders.HOST, getHostHeaderValue(request.getEndpoint())); // Copy over any other headers already in our request request.getHeaders().entrySet().stream() /* * HttpClient4 fills in the Content-Length header and complains if * it's already present, so we skip it here. We also skip the Host * header to avoid sending it twice, which will interfere with some * signing schemes. */ .filter(e -> !IGNORE_HEADERS.contains(e.getKey())) .forEach(e -> e.getValue().stream() .forEach(h -> httpRequest.addHeader(e.getKey(), h))); /* Set content type and encoding */ if (httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE) == null || httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE).length == 0) { httpRequest.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded; " + "charset=" + lowerCase(DEFAULT_ENCODING)); } }
/** * Configures the headers in the specified Apache HTTP request. */ private void addHeadersToRequest(HttpRequestBase httpRequest, Request<?> request) { httpRequest.addHeader(HttpHeaders.HOST, getHostHeaderValue(request.getEndpoint())); // Copy over any other headers already in our request for (Entry<String, String> entry : request.getHeaders().entrySet()) { /* * HttpClient4 fills in the Content-Length header and complains if * it's already present, so we skip it here. We also skip the Host * header to avoid sending it twice, which will interfere with some * signing schemes. */ if (!(ignoreHeaders.contains(entry.getKey()))) { httpRequest.addHeader(entry.getKey(), entry.getValue()); } } /* Set content type and encoding */ if (httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE) == null || httpRequest .getHeaders (HttpHeaders.CONTENT_TYPE).length == 0) { httpRequest.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded; " + "charset=" + DEFAULT_ENCODING.toLowerCase()); } }
/** * Configures the headers in the specified Apache HTTP request. */ private void addHeadersToRequest(HttpRequestBase httpRequest, Request<?> request) { httpRequest.addHeader(HttpHeaders.HOST, getHostHeaderValue(request.getEndpoint())); // Copy over any other headers already in our request for (Entry<String, String> entry : request.getHeaders().entrySet()) { /* * HttpClient4 fills in the Content-Length header and complains if * it's already present, so we skip it here. We also skip the Host * header to avoid sending it twice, which will interfere with some * signing schemes. */ if (!(ignoreHeaders.contains(entry.getKey()))) { httpRequest.addHeader(entry.getKey(), entry.getValue()); } } /* Set content type and encoding */ if (httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE) == null || httpRequest .getHeaders (HttpHeaders.CONTENT_TYPE).length == 0) { httpRequest.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded; " + "charset=" + DEFAULT_ENCODING.toLowerCase()); } }
private static void setBody(HttpRequest request, boolean async, HttpRequestBase reqObj) { if (!(request.getHttpMethod() == HttpMethod.GET || request.getHttpMethod() == HttpMethod.HEAD)) { if (request.getBody() != null) { HttpEntity entity = request.getBody().getEntity(); if (async) { if (reqObj.getHeaders(CONTENT_TYPE) == null || reqObj.getHeaders(CONTENT_TYPE).length == 0) { reqObj.setHeader(entity.getContentType()); } try { ByteArrayOutputStream output = new ByteArrayOutputStream(); entity.writeTo(output); NByteArrayEntity en = new NByteArrayEntity(output.toByteArray()); ((HttpEntityEnclosingRequestBase) reqObj).setEntity(en); } catch (IOException e) { throw new RuntimeException(e); } } else { ((HttpEntityEnclosingRequestBase) reqObj).setEntity(entity); } } } }
@Override public void interceptRequest(IHttpRequest theRequestInterface) { HttpRequestBase theRequest = ((ApacheHttpRequest) theRequestInterface).getApacheRequest(); if (theRequest instanceof HttpEntityEnclosingRequest) { Header[] encodingHeaders = theRequest.getHeaders(Constants.HEADER_CONTENT_ENCODING); if (encodingHeaders == null || encodingHeaders.length == 0) { HttpEntityEnclosingRequest req = (HttpEntityEnclosingRequest)theRequest; ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream gos; try { gos = new GZIPOutputStream(bos); req.getEntity().writeTo(gos); gos.finish(); } catch (IOException e) { ourLog.warn("Failed to GZip outgoing content", e); return; } byte[] byteArray = bos.toByteArray(); ByteArrayEntity newEntity = new ByteArrayEntity(byteArray); req.setEntity(newEntity); req.addHeader(Constants.HEADER_CONTENT_ENCODING, "gzip"); } } }
private void setBody(HttpRequestBase reqObj) { if (!(request.getHttpMethod() == GET || request.getHttpMethod() == HttpMethod.HEAD)) { if (request.getBody() != null) { HttpEntity entity = request.getBody().getEntity(); if (async) { if (reqObj.getHeaders(CONTENT_TYPE) == null || reqObj.getHeaders(CONTENT_TYPE).length == 0) { reqObj.setHeader(entity.getContentType()); } try { ByteArrayOutputStream output = new ByteArrayOutputStream(); entity.writeTo(output); NByteArrayEntity en = new NByteArrayEntity(output.toByteArray()); ((HttpEntityEnclosingRequestBase) reqObj).setEntity(en); } catch (IOException e) { throw new UnirestException(e); } } else { ((HttpEntityEnclosingRequestBase) reqObj).setEntity(entity); } } } } }
private void setBody(HttpRequestBase reqObj) { if (!(request.getHttpMethod() == GET || request.getHttpMethod() == HttpMethod.HEAD)) { if (request.getBody() != null) { HttpEntity entity = request.getBody().getEntity(); if (async) { if (reqObj.getHeaders(CONTENT_TYPE) == null || reqObj.getHeaders(CONTENT_TYPE).length == 0) { reqObj.setHeader(entity.getContentType()); } try { ByteArrayOutputStream output = new ByteArrayOutputStream(); entity.writeTo(output); NByteArrayEntity en = new NByteArrayEntity(output.toByteArray()); ((HttpEntityEnclosingRequestBase) reqObj).setEntity(en); } catch (IOException e) { throw new UnirestException(e); } } else { ((HttpEntityEnclosingRequestBase) reqObj).setEntity(entity); } } } } }
HttpEntity entity = request.getBody().getEntity(); if (async) { if (reqObj.getHeaders(CONTENT_TYPE) == null || reqObj.getHeaders(CONTENT_TYPE).length == 0) { reqObj.setHeader(entity.getContentType());
HttpEntity entity = request.getBody().getEntity(); if (async) { if (reqObj.getHeaders(CONTENT_TYPE) == null || reqObj.getHeaders(CONTENT_TYPE).length == 0) { reqObj.setHeader(entity.getContentType());
HttpEntity entity = request.getBody().getEntity(); if (async) { if (reqObj.getHeaders(HttpHeaders.CONTENT_TYPE) == null || reqObj.getHeaders(HttpHeaders.CONTENT_TYPE).length == 0) { reqObj.setHeader(entity.getContentType());
private static HttpRequestBase doEntityRequest(String url, String method, List<KeyValueStore> params) throws Exception { AbstractHttpEntity entity = null; if (isBodyEntity(params)) { Object entityBody = getEntityBody(params); if (isByteArray(entityBody)) entity = new ByteArrayEntity(getByteArray((Object[]) entityBody)); else if (isString(entityBody)) entity = new StringEntity((String) entityBody, "UTF-8"); else { throw new UnsupportedFilterResultType(String.format("filter result type %s is unsupported.", new Object[]{entityBody.getClass().getCanonicalName()})); } HttpRequestBase request = doEntityRequest(url, method, entity); Header[] headers = request.getHeaders("Content-Type"); if (headers.length == 0) { request.setHeader("Content-Type", "application/json"); } return request; } HttpParameter httpParameter = getParameters(convertRequestParameter(params)); entity = new UrlEncodedFormEntity(httpParameter.getNameValuePairs(), "UTF-8"); return doEntityRequest(url, method, entity); }