List of usage examples for com.squareup.okhttp Request headers
Headers headers
To view the source code for com.squareup.okhttp Request headers.
Click Source Link
From source file:com.magnet.max.android.rest.qos.internal.CachedRequest.java
License:Apache License
public CachedRequest(Request request) { url = request.urlString();/* w w w . j a v a 2s.co m*/ method = request.method(); parseHeaders(request.headers()); if (null != request.body()) { try { Buffer buffer = new Buffer(); request.body().writeTo(buffer); body = CacheUtils.copyBody(buffer); } catch (IOException e) { e.printStackTrace(); } } }
From source file:com.magnet.max.android.rest.qos.internal.CacheUtils.java
License:Apache License
public static String getRequestHash(Request request) { StringBuilder sb = new StringBuilder(); //Method and URL sb.append(request.method()).append(request.urlString()); //Headers/*from w w w. jav a2 s . c o m*/ if (null != request.headers()) { } //Body //Don't include body when it's multipart if (toHashBody(request)) { try { Buffer buffer = new Buffer(); Request copy = request.newBuilder().build(); copy.body().writeTo(buffer); sb.append(buffer.readUtf8()); } catch (IOException e) { e.printStackTrace(); } } return Util.md5Hex(sb.toString()); }
From source file:com.navercorp.pinpoint.plugin.okhttp.interceptor.HttpEngineSendRequestMethodInterceptor.java
License:Apache License
private void recordCookie(Request request, Trace trace) { for (String cookie : request.headers("Cookie")) { if (cookieSampler.isSampling()) { final SpanEventRecorder recorder = trace.currentSpanEventRecorder(); recorder.recordAttribute(AnnotationKey.HTTP_COOKIE, StringUtils.abbreviate(cookie, 1024)); }//from ww w . jav a2s . c om return; } }
From source file:com.navercorp.pinpoint.plugin.okhttp.v2.OkHttpCookieExtractor.java
License:Apache License
@Override public String getCookie(Request request) { for (String cookie : request.headers("Cookie")) { if (StringUtils.hasLength(cookie)) { return cookie; }// www . jav a 2 s. c o m } return null; }
From source file:com.parse.ParseOkHttpClient.java
License:Open Source License
private ParseHttpRequest getParseHttpRequest(Request okHttpRequest) { ParseHttpRequest.Builder parseRequestBuilder = new ParseHttpRequest.Builder(); // Set method switch (okHttpRequest.method()) { case OKHTTP_GET: parseRequestBuilder.setMethod(ParseHttpRequest.Method.GET); break;/*from w w w . j a v a 2s . c o m*/ case OKHTTP_DELETE: parseRequestBuilder.setMethod(ParseHttpRequest.Method.DELETE); break; case OKHTTP_POST: parseRequestBuilder.setMethod(ParseHttpRequest.Method.POST); break; case OKHTTP_PUT: parseRequestBuilder.setMethod(ParseHttpRequest.Method.PUT); break; default: // This should never happen throw new IllegalArgumentException("Invalid http method " + okHttpRequest.method()); } // Set url parseRequestBuilder.setUrl(okHttpRequest.urlString()); // Set Header for (Map.Entry<String, List<String>> entry : okHttpRequest.headers().toMultimap().entrySet()) { parseRequestBuilder.addHeader(entry.getKey(), entry.getValue().get(0)); } // Set Body ParseOkHttpRequestBody okHttpBody = (ParseOkHttpRequestBody) okHttpRequest.body(); if (okHttpBody != null) { parseRequestBuilder.setBody(okHttpBody.getParseHttpBody()); } return parseRequestBuilder.build(); }
From source file:com.parse.ParseOkHttpClientTest.java
License:Open Source License
@Test public void testGetOkHttpRequest() throws IOException { Map<String, String> headers = new HashMap<>(); String headerName = "name"; String headerValue = "value"; headers.put(headerName, headerValue); String url = "http://www.parse.com/"; String content = "test"; int contentLength = content.length(); String contentType = "application/json"; ParseHttpRequest parseRequest = new ParseHttpRequest.Builder().setUrl(url) .setMethod(ParseHttpRequest.Method.POST).setBody(new ParseByteArrayHttpBody(content, contentType)) .setHeaders(headers).build(); ParseOkHttpClient parseClient = new ParseOkHttpClient(10000, null); Request okHttpRequest = parseClient.getRequest(parseRequest); // Verify method assertEquals(ParseHttpRequest.Method.POST.toString(), okHttpRequest.method()); // Verify URL assertEquals(url, okHttpRequest.urlString()); // Verify Headers assertEquals(1, okHttpRequest.headers(headerName).size()); assertEquals(headerValue, okHttpRequest.headers(headerName).get(0)); // Verify Body RequestBody okHttpBody = okHttpRequest.body(); assertEquals(contentLength, okHttpBody.contentLength()); assertEquals(contentType, okHttpBody.contentType().toString()); // Can not read parseRequest body to compare since it has been read during // creating okHttpRequest Buffer buffer = new Buffer(); okHttpBody.writeTo(buffer);//from w ww .j av a 2 s .c o m ByteArrayOutputStream output = new ByteArrayOutputStream(); buffer.copyTo(output); assertArrayEquals(content.getBytes(), output.toByteArray()); }
From source file:com.rafagarcia.countries.backend.webapi.HttpLoggingInterceptor.java
License:Apache License
@Override public Response intercept(Chain chain) throws IOException { Level level = this.level; Request request = chain.request(); if (level == Level.NONE) { return chain.proceed(request); }/*from w ww .j a v a 2 s . c o m*/ boolean logBody = level == Level.BODY; boolean logHeaders = logBody || level == Level.HEADERS; RequestBody requestBody = request.body(); boolean hasRequestBody = requestBody != null; Connection connection = chain.connection(); Protocol protocol = connection != null ? connection.getProtocol() : Protocol.HTTP_1_1; String requestStartMessage = "--> " + request.method() + ' ' + requestPath(request.httpUrl()) + ' ' + protocol(protocol); if (!logHeaders && hasRequestBody) { requestStartMessage += " (" + requestBody.contentLength() + "-byte body)"; } logger.log(requestStartMessage); if (logHeaders) { Headers headers = request.headers(); for (int i = 0, count = headers.size(); i < count; i++) { logger.log(headers.name(i) + ": " + headers.value(i)); } if (logBody && hasRequestBody) { Buffer buffer = new Buffer(); requestBody.writeTo(buffer); Charset charset = UTF8; MediaType contentType = requestBody.contentType(); if (contentType != null) { contentType.charset(UTF8); } logger.log(""); logger.log(buffer.readString(charset)); } String endMessage = "--> END " + request.method(); if (logBody && hasRequestBody) { endMessage += " (" + requestBody.contentLength() + "-byte body)"; } logger.log(endMessage); } long startNs = System.nanoTime(); Response response = chain.proceed(request); long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs); ResponseBody responseBody = response.body(); logger.log("<-- " + protocol(response.protocol()) + ' ' + response.code() + ' ' + response.message() + " (" + tookMs + "ms" + (!logHeaders ? ", " + responseBody.contentLength() + "-byte body" : "") + ')'); if (logHeaders) { Headers headers = response.headers(); for (int i = 0, count = headers.size(); i < count; i++) { logger.log(headers.name(i) + ": " + headers.value(i)); } if (logBody) { Buffer buffer = new Buffer(); responseBody.source().readAll(buffer); Charset charset = UTF8; MediaType contentType = responseBody.contentType(); if (contentType != null) { charset = contentType.charset(UTF8); } if (responseBody.contentLength() > 0) { logger.log(""); logger.log(buffer.clone().readString(charset)); } // Since we consumed the original, replace the one-shot body in the response with a new one. response = response.newBuilder() .body(ResponseBody.create(contentType, responseBody.contentLength(), buffer)).build(); } String endMessage = "<-- END HTTP"; if (logBody) { endMessage += " (" + responseBody.contentLength() + "-byte body)"; } logger.log(endMessage); } return response; }
From source file:com.ringcentral.rc_android_sdk.rcsdk.http.Client.java
License:Open Source License
public Headers getRequestHeader(Request request) { return request.headers(); }
From source file:com.spotify.apollo.http.client.HttpClient.java
License:Apache License
@Override public CompletionStage<com.spotify.apollo.Response<ByteString>> send(com.spotify.apollo.Request apolloRequest, Optional<com.spotify.apollo.Request> apolloIncomingRequest) { final Optional<RequestBody> requestBody = apolloRequest.payload().map(payload -> { final MediaType contentType = apolloRequest.header("Content-Type").map(MediaType::parse) .orElse(DEFAULT_CONTENT_TYPE); return RequestBody.create(contentType, payload); });/*from ww w .j a v a 2 s . com*/ Headers.Builder headersBuilder = new Headers.Builder(); apolloRequest.headers().asMap().forEach((k, v) -> headersBuilder.add(k, v)); apolloIncomingRequest.flatMap(req -> req.header(AUTHORIZATION_HEADER)) .ifPresent(header -> headersBuilder.add(AUTHORIZATION_HEADER, header)); final Request request = new Request.Builder().method(apolloRequest.method(), requestBody.orElse(null)) .url(apolloRequest.uri()).headers(headersBuilder.build()).build(); final CompletableFuture<com.spotify.apollo.Response<ByteString>> result = new CompletableFuture<>(); //https://github.com/square/okhttp/wiki/Recipes#per-call-configuration OkHttpClient finalClient = client; if (apolloRequest.ttl().isPresent() && client.getReadTimeout() != apolloRequest.ttl().get().toMillis()) { finalClient = client.clone(); finalClient.setReadTimeout(apolloRequest.ttl().get().toMillis(), TimeUnit.MILLISECONDS); } finalClient.newCall(request).enqueue(TransformingCallback.create(result)); return result; }
From source file:com.spotify.apollo.http.server.HttpServerModuleTest.java
License:Apache License
@Test public void testParsesHeadersParameters() throws Exception { int port = 9085; try (Service.Instance instance = service().start(NO_ARGS, onPort(port))) { HttpServer server = HttpServerModule.server(instance); TestHandler testHandler = new TestHandler(); server.start(testHandler);/*from w w w .j a va 2s .c om*/ Request httpRequest = new Request.Builder().get().url(baseUrl(port) + "/headers") .addHeader("Foo", "bar").addHeader("Repeat", "once").addHeader("Repeat", "twice").build(); com.squareup.okhttp.Response response = okHttpClient.newCall(httpRequest).execute(); assertThat(response.code(), is(IM_A_TEAPOT.code())); assertThat(testHandler.requests.size(), is(1)); final com.spotify.apollo.Request apolloRequest = testHandler.requests.get(0).request(); assertThat(apolloRequest.uri(), is("/headers")); assertThat(apolloRequest.header("Foo"), is(Optional.of("bar"))); assertThat(apolloRequest.header("Repeat"), is(Optional.of("once,twice"))); assertThat(apolloRequest.header("Baz"), is(Optional.empty())); System.out.println("apolloRequest.headers() = " + apolloRequest.headers()); } }