Search in sources :

Example 91 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project okhttp by square.

the class HttpOverHttp2Test method userSuppliedContentLengthHeader.

@Test
public void userSuppliedContentLengthHeader() throws Exception {
    final byte[] postBytes = "FGHIJ".getBytes(Util.UTF_8);
    server.enqueue(new MockResponse().setBody("ABCDE"));
    Call call = client.newCall(new Request.Builder().url(server.url("/foo")).post(new RequestBody() {

        @Override
        public MediaType contentType() {
            return MediaType.parse("text/plain; charset=utf-8");
        }

        @Override
        public long contentLength() throws IOException {
            return postBytes.length;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.write(postBytes);
        }
    }).build());
    Response response = call.execute();
    assertEquals("ABCDE", response.body().string());
    RecordedRequest request = server.takeRequest();
    assertEquals("POST /foo HTTP/1.1", request.getRequestLine());
    assertArrayEquals(postBytes, request.getBody().readByteArray());
    assertEquals(postBytes.length, Integer.parseInt(request.getHeader("Content-Length")));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) MediaType(okhttp3.MediaType) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 92 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project camel by apache.

the class MultiPartFormOkHttpTest method createMultipartRequest.

private Request createMultipartRequest() throws Exception {
    MediaType mediaType = MediaType.parse("multipart/form-data; boundary=---011000010111000001101001");
    RequestBody body = RequestBody.create(mediaType, "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"test\"\r\n\r\nsome data here\r\n-----011000010111000001101001--");
    Request request = new Request.Builder().url("http://localhost:" + getPort() + "/test").post(body).addHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001").addHeader("cache-control", "no-cache").addHeader("postman-token", "a9fd95b6-04b9-ea7a-687e-ff828ea00774").build();
    return request;
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) Request(okhttp3.Request) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Example 93 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project WeexErosFramework by bmfe.

the class WeexOkhttp3Interceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    String requestId = String.valueOf(mNextRequestId.getAndIncrement());
    Request request = chain.request();
    mEventReporter = NetworkEventReporterManager.get();
    RequestBodyHelper requestBodyHelper = null;
    if (mEventReporter != null) {
        requestBodyHelper = new RequestBodyHelper(mEventReporter, requestId);
        OkHttpInspectorRequest inspectorRequest = new OkHttpInspectorRequest(requestId, request, requestBodyHelper);
        mEventReporter.requestWillBeSent(inspectorRequest);
    }
    Response response;
    try {
        response = chain.proceed(request);
    } catch (IOException e) {
        if (mEventReporter != null) {
            mEventReporter.httpExchangeFailed(requestId, e.toString());
        }
        throw e;
    }
    if (mEventReporter != null) {
        if (requestBodyHelper.hasBody()) {
            requestBodyHelper.reportDataSent();
        }
        Connection connection = chain.connection();
        mEventReporter.responseHeadersReceived(new OkHttpInspectorResponse(requestId, request, response));
        ResponseBody body = response.body();
        MediaType contentType = null;
        InputStream responseStream = null;
        if (body != null) {
            contentType = body.contentType();
            responseStream = body.byteStream();
        }
        responseStream = mEventReporter.interpretResponseStream(requestId, contentType != null ? contentType.toString() : null, response.header("Content-Encoding"), responseStream, new DefaultResponseHandler(mEventReporter, requestId));
        if (responseStream != null) {
            response = response.newBuilder().body(new ForwardingResponseBody(body, responseStream)).build();
        }
    }
    return response;
}
Also used : InputStream(java.io.InputStream) Request(okhttp3.Request) Connection(okhttp3.Connection) IOException(java.io.IOException) DefaultResponseHandler(com.taobao.weex.devtools.inspector.network.DefaultResponseHandler) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) RequestBodyHelper(com.taobao.weex.devtools.inspector.network.RequestBodyHelper) MediaType(okhttp3.MediaType)

Example 94 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project WeexErosFramework by bmfe.

the class AxiosManager method createRequestBodyByMediaType.

private RequestBody createRequestBodyByMediaType(Map<String, String> header, String content) {
    if (header != null && !TextUtils.isEmpty(header.get("Content-Type"))) {
        String s = header.get("Content-Type");
        MediaType mediaType = null;
        try {
            mediaType = MediaType.parse(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (mediaType == null) {
            mediaType = MediaType.parse(DEFAULT_MEDIATYPE);
        }
        return RequestBody.create(mediaType, content);
    }
    return RequestBody.create(MediaType.parse(DEFAULT_MEDIATYPE), content);
}
Also used : MediaType(okhttp3.MediaType) IrregularUrlException(com.benmu.framework.http.okhttp.exception.IrregularUrlException)

Example 95 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project couchbase-lite-android by couchbase.

the class ReplicatorWithSyncGatewayTest method getSessionAuthenticatorFromSG.

SessionAuthenticator getSessionAuthenticatorFromSG() throws Exception {
    // Obtain Sync-Gateway Session ID
    final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    OkHttpClient client = new OkHttpClient();
    String url = String.format(Locale.ENGLISH, "http://%s:4985/seekrit/_session", config.remoteHost());
    RequestBody body = RequestBody.create(JSON, "{\"name\": \"pupshaw\"}");
    Request request = new Request.Builder().url(url).post(body).build();
    Response response = client.newCall(request).execute();
    String respBody = response.body().string();
    Log.e(TAG, "json string -> " + respBody);
    JSONObject json = new JSONObject(respBody);
    return new SessionAuthenticator(json.getString("session_id"), json.getString("cookie_name"));
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) JSONObject(org.json.JSONObject) Request(okhttp3.Request) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Aggregations

MediaType (okhttp3.MediaType)297 RequestBody (okhttp3.RequestBody)186 Request (okhttp3.Request)179 Response (okhttp3.Response)158 IOException (java.io.IOException)137 ResponseBody (okhttp3.ResponseBody)71 OkHttpClient (okhttp3.OkHttpClient)68 Charset (java.nio.charset.Charset)50 Buffer (okio.Buffer)50 Headers (okhttp3.Headers)48 JSONObject (org.json.JSONObject)38 BufferedSource (okio.BufferedSource)36 MultipartBody (okhttp3.MultipartBody)34 HttpUrl (okhttp3.HttpUrl)30 Map (java.util.Map)24 BufferedSink (okio.BufferedSink)23 File (java.io.File)22 InputStream (java.io.InputStream)21 ArrayList (java.util.ArrayList)21 Test (org.junit.Test)21