Search in sources :

Example 16 with Headers

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());
}
Also used : Request(okhttp3.Request) OAuthRequest(com.github.scribejava.core.model.OAuthRequest) MediaType(okhttp3.MediaType) HashMap(java.util.HashMap) Map(java.util.Map) RequestBody(okhttp3.RequestBody)

Example 17 with Headers

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());
}
Also used : Response(com.github.scribejava.core.model.Response) HashMap(java.util.HashMap) Headers(okhttp3.Headers)

Example 18 with Headers

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());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HttpHeaders(org.springframework.http.HttpHeaders) Test(org.junit.Test)

Example 19 with Headers

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);
        }
    });
}
Also used : Response(okhttp3.Response) ANResponse(com.androidnetworking.common.ANResponse) AnalyticsListener(com.androidnetworking.interfaces.AnalyticsListener) JSONArray(org.json.JSONArray) ANError(com.androidnetworking.error.ANError) OkHttpResponseAndJSONArrayRequestListener(com.androidnetworking.interfaces.OkHttpResponseAndJSONArrayRequestListener)

Example 20 with Headers

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);
        }
    });
}
Also used : Response(okhttp3.Response) ANResponse(com.androidnetworking.common.ANResponse) AnalyticsListener(com.androidnetworking.interfaces.AnalyticsListener) JSONObject(org.json.JSONObject) ANError(com.androidnetworking.error.ANError) OkHttpResponseAndJSONObjectRequestListener(com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener)

Aggregations

Test (org.junit.Test)69 Request (okhttp3.Request)55 Response (okhttp3.Response)50 Headers (okhttp3.Headers)43 MockResponse (okhttp3.mockwebserver.MockResponse)33 IOException (java.io.IOException)32 HttpHeaders (okhttp3.internal.http.HttpHeaders)27 ResponseBody (okhttp3.ResponseBody)25 List (java.util.List)19 RequestBody (okhttp3.RequestBody)19 HashMap (java.util.HashMap)14 Map (java.util.Map)14 ANResponse (com.androidnetworking.common.ANResponse)13 AnalyticsListener (com.androidnetworking.interfaces.AnalyticsListener)13 LinkedHashMap (java.util.LinkedHashMap)13 MediaType (okhttp3.MediaType)13 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)13 ANError (com.androidnetworking.error.ANError)11 HttpURLConnection (java.net.HttpURLConnection)11 JSONObject (org.json.JSONObject)11