/** * Returns a media type for {@code string}, or null if {@code string} is not a well-formed media * type. */ public static @Nullable MediaType parse(String string) { try { return get(string); } catch (IllegalArgumentException ignored) { return null; } }
void addHeader(String name, String value) { if ("Content-Type".equalsIgnoreCase(name)) { try { contentType = MediaType.get(value); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Malformed content type: " + value, e); } } else { requestBuilder.addHeader(name, value); } }
/** * Returns a media type for {@code string}, or null if {@code string} is not a well-formed media * type. */ public static @Nullable MediaType parse(String string) { try { return get(string); } catch (IllegalArgumentException ignored) { return null; } }
void addHeader(String name, String value) { if ("Content-Type".equalsIgnoreCase(name)) { try { contentType = MediaType.get(value); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Malformed content type: " + value, e); } } else { requestBuilder.addHeader(name, value); } }
MultipartBody(ByteString boundary, MediaType type, List<Part> parts) { this.boundary = boundary; this.originalType = type; this.contentType = MediaType.get(type + "; boundary=" + boundary.utf8()); this.parts = Util.immutableList(parts); }
private Headers parseHeaders(String[] headers) { Headers.Builder builder = new Headers.Builder(); for (String header : headers) { int colon = header.indexOf(':'); if (colon == -1 || colon == 0 || colon == header.length() - 1) { throw methodError(method, "@Headers value must be in the form \"Name: Value\". Found: \"%s\"", header); } String headerName = header.substring(0, colon); String headerValue = header.substring(colon + 1).trim(); if ("Content-Type".equalsIgnoreCase(headerName)) { try { contentType = MediaType.get(headerValue); } catch (IllegalArgumentException e) { throw methodError(method, e, "Malformed content type: %s", headerValue); } } else { builder.add(headerName, headerValue); } } return builder.build(); }
MultipartBody(ByteString boundary, MediaType type, List<Part> parts) { this.boundary = boundary; this.originalType = type; this.contentType = MediaType.get(type + "; boundary=" + boundary.utf8()); this.parts = Util.immutableList(parts); }
private Headers parseHeaders(String[] headers) { Headers.Builder builder = new Headers.Builder(); for (String header : headers) { int colon = header.indexOf(':'); if (colon == -1 || colon == 0 || colon == header.length() - 1) { throw methodError(method, "@Headers value must be in the form \"Name: Value\". Found: \"%s\"", header); } String headerName = header.substring(0, colon); String headerValue = header.substring(colon + 1).trim(); if ("Content-Type".equalsIgnoreCase(headerName)) { try { contentType = MediaType.get(headerValue); } catch (IllegalArgumentException e) { throw methodError(method, e, "Malformed content type: %s", headerValue); } } else { builder.add(headerName, headerValue); } } return builder.build(); }
/** * Returns a media type for {@code string}, or null if {@code string} is not a well-formed media * type. */ public static @Nullable MediaType parse(String string) { try { return get(string); } catch (IllegalArgumentException ignored) { return null; } }
@Override public String call(String json) throws IOException { Request request = new Request.Builder() .url("http://localhost:" + port + "/analyze") .post(RequestBody.create(MediaType.get("application/json"), json)) .build(); try (Response response = client.newCall(request).execute()) { // in this case response.body() is never null (according to docs) return response.body().string(); } }
@Override public String call(String json) throws IOException { Request request = new Request.Builder() .url("http://localhost:" + port + "/analyze") .post(RequestBody.create(MediaType.get("application/json"), json)) .build(); try (Response response = client.newCall(request).execute()) { // in this case response.body() is never null (according to docs) return response.body().string(); } }
public static <T> MeshOkHttpReqeuestImpl<T> BinaryRequest(OkHttpClient client, String method, String url, Map<String, String> headers, Class<? extends T> classOfT, byte[] bodyData, String contentType) { Request request = createBaseBuilder(url, headers, method, RequestBody.create(MediaType.get(contentType), bodyData)).build(); return new MeshOkHttpReqeuestImpl<>(client, request, classOfT); }
public static <T> MeshOkHttpReqeuestImpl<T> JsonRequest(OkHttpClient client, String method, String url, Map<String, String> headers, Class<? extends T> classOfT, String json) { Request request = createBaseBuilder(url, headers, method, RequestBody.create(MediaType.get("application/json"), json)).build(); return new MeshOkHttpReqeuestImpl<>(client, request, classOfT); }
MultipartBody(ByteString boundary, MediaType type, List<Part> parts) { this.boundary = boundary; this.originalType = type; this.contentType = MediaType.get(type + "; boundary=" + boundary.utf8()); this.parts = Util.immutableList(parts); }
MultipartBody(ByteString boundary, MediaType type, List<Part> parts) { this.boundary = boundary; this.originalType = type; this.contentType = MediaType.get(type + "; boundary=" + boundary.utf8()); this.parts = Util.immutableList(parts); }
MultipartBody(ByteString boundary, MediaType type, List<Part> parts) { this.boundary = boundary; this.originalType = type; this.contentType = MediaType.get(type + "; boundary=" + boundary.utf8()); this.parts = Util.immutableList(parts); }
@Override public Response intercept(final Interceptor.Chain chain) throws IOException { Response response = chain.proceed(chain.request()); final MediaType originalContentType = response.body().contentType(); if (originalContentType != null && "text".equalsIgnoreCase(originalContentType.type()) && "xml".equalsIgnoreCase(originalContentType.subtype()) && originalContentType.charset() == null) { final String peek = response.peekBody(64).string(); final Matcher matcher = P_XML_PRAGMA.matcher(peek); if (matcher.find()) { final String encoding = matcher.group(1); final MediaType contentType = MediaType.get(originalContentType.type() + '/' + originalContentType.subtype() + ";charset=" + encoding); final ResponseBody body = response.body(); final Builder responseBuilder = response.newBuilder(); responseBuilder.header(HEADER_CONTENT_TYPE, contentType.toString()); responseBuilder.body(ResponseBody.create(contentType, body.contentLength(), body.source())); response = responseBuilder.build(); log.debug("Deriving missing {} encoding from XML pragma", encoding); } } return response; } };
.post(RequestBody.create(MediaType.get(RestApiConstants.CONTENT_TYPE_JSON), requestBody)).build()).execute();