use of okhttp3.Headers in project scribejava by scribejava.
the class OkHttpHttpClient method createCall.
private Call createCall(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, BodyType bodyType, Object bodyContents) {
final Request.Builder requestBuilder = new Request.Builder();
requestBuilder.url(completeUrl);
final String method = httpVerb.name();
// prepare body
final RequestBody body;
if (bodyContents != null && HttpMethod.permitsRequestBody(method)) {
final MediaType mediaType = headers.containsKey(CONTENT_TYPE) ? MediaType.parse(headers.get(CONTENT_TYPE)) : DEFAULT_CONTENT_TYPE_MEDIA_TYPE;
body = bodyType.createBody(mediaType, bodyContents);
} else {
body = null;
}
// fill HTTP method and body
requestBuilder.method(method, body);
// fill headers
for (Map.Entry<String, String> header : headers.entrySet()) {
requestBuilder.addHeader(header.getKey(), header.getValue());
}
if (userAgent != null) {
requestBuilder.header(OAuthConstants.USER_AGENT_HEADER_NAME, userAgent);
}
// create a new call
return client.newCall(requestBuilder.build());
}
use of okhttp3.Headers in project scribejava by scribejava.
the class OkHttpHttpClient method convertResponse.
static Response convertResponse(okhttp3.Response okHttpResponse) {
final Headers headers = okHttpResponse.headers();
final Map<String, String> headersMap = new HashMap<>();
for (String name : headers.names()) {
headersMap.put(name, headers.get(name));
}
return new Response(okHttpResponse.code(), okHttpResponse.message(), headersMap, okHttpResponse.body().byteStream());
}
use of okhttp3.Headers in project spring-framework by spring-projects.
the class WebClientIntegrationTests method headers.
@Test
public void headers() throws Exception {
this.server.enqueue(new MockResponse().setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
Mono<HttpHeaders> result = this.webClient.get().uri("/greeting?name=Spring").exchange().map(response -> response.headers().asHttpHeaders());
StepVerifier.create(result).consumeNextWith(httpHeaders -> {
assertEquals(MediaType.TEXT_PLAIN, httpHeaders.getContentType());
assertEquals(13L, httpHeaders.getContentLength());
}).expectComplete().verify(Duration.ofSeconds(3));
RecordedRequest recordedRequest = server.takeRequest();
Assert.assertEquals(1, server.getRequestCount());
Assert.assertEquals("*/*", recordedRequest.getHeader(HttpHeaders.ACCEPT));
Assert.assertEquals("/greeting?name=Spring", recordedRequest.getPath());
}
use of okhttp3.Headers in project Fast-Android-Networking by amitshekhariitbhu.
the class OkHttpResponseTestActivity method getResponseOnlyFromNetwork.
public void getResponseOnlyFromNetwork(View view) {
AndroidNetworking.get(ApiEndPoint.BASE_URL + ApiEndPoint.GET_JSON_ARRAY).addPathParameter("pageNumber", "0").addQueryParameter("limit", "3").setTag(this).setPriority(Priority.LOW).getResponseOnlyFromNetwork().build().setAnalyticsListener(new AnalyticsListener() {
@Override
public void onReceived(long timeTakenInMillis, long bytesSent, long bytesReceived, boolean isFromCache) {
Log.d(TAG, " timeTakenInMillis : " + timeTakenInMillis);
Log.d(TAG, " bytesSent : " + bytesSent);
Log.d(TAG, " bytesReceived : " + bytesReceived);
Log.d(TAG, " isFromCache : " + isFromCache);
}
}).getAsOkHttpResponseAndJSONArray(new OkHttpResponseAndJSONArrayRequestListener() {
@Override
public void onResponse(Response okHttpResponse, JSONArray response) {
Log.d(TAG, "onResponse object : " + response.toString());
Log.d(TAG, "onResponse isMainThread : " + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
if (okHttpResponse.isSuccessful()) {
Log.d(TAG, "onResponse success headers : " + okHttpResponse.headers().toString());
} else {
Log.d(TAG, "onResponse not success headers : " + okHttpResponse.headers().toString());
}
}
@Override
public void onError(ANError anError) {
Utils.logError(TAG, anError);
}
});
}
use of okhttp3.Headers in project Fast-Android-Networking by amitshekhariitbhu.
the class OkHttpResponseTestActivity method createAnUser.
public void createAnUser(View view) {
AndroidNetworking.post(ApiEndPoint.BASE_URL + ApiEndPoint.POST_CREATE_AN_USER).addBodyParameter("firstname", "Suman").addBodyParameter("lastname", "Shekhar").setTag(this).setPriority(Priority.LOW).build().setAnalyticsListener(new AnalyticsListener() {
@Override
public void onReceived(long timeTakenInMillis, long bytesSent, long bytesReceived, boolean isFromCache) {
Log.d(TAG, " timeTakenInMillis : " + timeTakenInMillis);
Log.d(TAG, " bytesSent : " + bytesSent);
Log.d(TAG, " bytesReceived : " + bytesReceived);
Log.d(TAG, " isFromCache : " + isFromCache);
}
}).getAsOkHttpResponseAndJSONObject(new OkHttpResponseAndJSONObjectRequestListener() {
@Override
public void onResponse(Response okHttpResponse, JSONObject response) {
Log.d(TAG, "onResponse object : " + response.toString());
Log.d(TAG, "onResponse isMainThread : " + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
if (okHttpResponse.isSuccessful()) {
Log.d(TAG, "onResponse success headers : " + okHttpResponse.headers().toString());
} else {
Log.d(TAG, "onResponse not success headers : " + okHttpResponse.headers().toString());
}
}
@Override
public void onError(ANError anError) {
Utils.logError(TAG, anError);
}
});
}
Aggregations