List of usage examples for com.squareup.okhttp RequestBody create
public static RequestBody create(final MediaType contentType, final byte[] content, final int offset, final int byteCount)
From source file:com.facebook.buck.slb.ThriftOverHttpService.java
License:Apache License
@Override public void makeRequest(ThriftRequest thriftRequest, ThriftResponse thriftResponse) throws IOException { if (LOG.isVerboseEnabled()) { LOG.verbose("Making Thrift Request: [%s].", thriftToDebugJson(thriftRequest)); }//from w w w . j ava 2 s .c o m // Prepare the request. // TODO(ruibm): Move to use a TTransport that won't need to keep both serialized and raw // data in memory. TSerializer serializer = new TSerializer(config.getThriftProtocol().getFactory()); byte[] serializedBytes = null; try { serializedBytes = serializer.serialize(thriftRequest); } catch (TException e) { throw new ThriftServiceException("Problems serializing the thrift struct.", e); } RequestBody body = RequestBody.create(THRIFT_CONTENT_TYPE, serializedBytes, 0, serializedBytes.length); Request.Builder requestBuilder = new Request.Builder() .addHeader(THRIFT_PROTOCOL_HEADER, config.getThriftProtocol().toString().toLowerCase()).post(body); // Make the HTTP request and handle the response status code. try (HttpResponse response = config.getService().makeRequest(config.getThriftPath(), requestBuilder)) { if (response.code() != 200) { throw new ThriftServiceException( String.format("HTTP response returned unexpected status code [%d] from URL [%s].", response.code(), response.requestUrl())); } // Deserialize the body payload into the thrift struct. try (TIOStreamTransport responseTransport = new TIOStreamTransport(response.getBody())) { TProtocol responseProtocol = newProtocolInstance(config.getThriftProtocol(), responseTransport); thriftResponse.read(responseProtocol); if (LOG.isVerboseEnabled()) { LOG.debug("Received Thrift Response: [%s].", thriftToDebugJson(thriftResponse)); } } catch (TException e) { throw new ThriftServiceException("Failed to deserialize the thrift body payload.", e); } } }