@Override public RequestBody convert(T value) throws IOException { return RequestBody.create(MEDIA_TYPE, String.valueOf(value)); } }
/** Returns a new request body that transmits {@code content}. */ public static RequestBody create(final @Nullable MediaType contentType, final byte[] content) { return create(contentType, content, 0, content.length); }
String post(String url, String json) throws IOException { RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder() .url(url) .post(body) .build(); try (Response response = client.newCall(request).execute()) { return response.body().string(); } }
public void run() throws Exception { File file = new File("README.md"); Request request = new Request.Builder() .url("https://api.github.com/markdown/raw") .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file)) .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); } }
@Override public RequestBody convert(T value) throws IOException { byte[] bytes = value.toByteArray(); return RequestBody.create(MEDIA_TYPE, bytes); } }
@Override public RequestBody convert(T value) throws IOException { byte[] bytes = adapter.writeValueAsBytes(value); return RequestBody.create(MEDIA_TYPE, bytes); } }
private RequestBody getRequestBody() { if (data == null) { return null; } String bodyData = data; String mimeType = "application/x-www-form-urlencoded"; if (headers != null) { for (String header : headers) { String[] parts = header.split(":", -1); if ("Content-Type".equalsIgnoreCase(parts[0])) { mimeType = parts[1].trim(); headers.remove(header); break; } } } return RequestBody.create(MediaType.parse(mimeType), bodyData); }
public static Part createFormData(String name, String value) { return createFormData(name, null, RequestBody.create(null, value)); }
public void run() throws Exception { Map<String, String> requestBody = new LinkedHashMap<>(); requestBody.put("longUrl", "https://publicobject.com/2014/12/04/html-formatting-javadocs/"); RequestBody jsonRequestBody = RequestBody.create( MEDIA_TYPE_JSON, mapJsonAdapter.toJson(requestBody)); Request request = new Request.Builder() .url("https://www.googleapis.com/urlshortener/v1/url?key=" + GOOGLE_API_KEY) .post(jsonRequestBody) .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); } }
public void run() throws Exception { String postBody = "" + "Releases\n" + "--------\n" + "\n" + " * _1.0_ May 6, 2013\n" + " * _1.1_ June 15, 2013\n" + " * _1.2_ August 11, 2013\n"; Request request = new Request.Builder() .url("https://api.github.com/markdown/raw") .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody)) .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); } }
/** * Returns a new request body that transmits {@code content}. If {@code contentType} is non-null * and lacks a charset, this will use UTF-8. */ public static RequestBody create(@Nullable MediaType contentType, String content) { Charset charset = UTF_8; if (contentType != null) { charset = contentType.charset(); if (charset == null) { charset = UTF_8; contentType = MediaType.parse(contentType + "; charset=utf-8"); } } byte[] bytes = content.getBytes(charset); return create(contentType, bytes); }
@Override public RequestBody convert(final T value) throws IOException { Buffer buffer = new Buffer(); try { Marshaller marshaller = context.createMarshaller(); XMLStreamWriter xmlWriter = xmlOutputFactory.createXMLStreamWriter( buffer.outputStream(), JaxbConverterFactory.XML.charset().name()); marshaller.marshal(value, xmlWriter); } catch (JAXBException | XMLStreamException e) { throw new RuntimeException(e); } return RequestBody.create(JaxbConverterFactory.XML, buffer.readByteString()); } }
@Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); adapter.encode(buffer, value); return RequestBody.create(MEDIA_TYPE, buffer.snapshot()); } }
private Request buildRequest(String hostname, int type) { Request.Builder requestBuilder = new Request.Builder().header("Accept", DNS_MESSAGE.toString()); ByteString query = DnsRecordCodec.encodeQuery(hostname, type); if (post) { requestBuilder = requestBuilder.url(url).post(RequestBody.create(DNS_MESSAGE, query)); } else { String encoded = query.base64Url().replace("=", ""); HttpUrl requestUrl = url.newBuilder().addQueryParameter("dns", encoded).build(); requestBuilder = requestBuilder.url(requestUrl); } return requestBuilder.build(); }
@Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); try { OutputStreamWriter osw = new OutputStreamWriter(buffer.outputStream(), CHARSET); serializer.write(value, osw); osw.flush(); } catch (RuntimeException | IOException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); } }
public void run() throws Exception { // Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("title", "Square Logo") .addFormDataPart("image", "logo-square.png", RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png"))) .build(); Request request = new Request.Builder() .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID) .url("https://api.imgur.com/3/image") .post(requestBody) .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); } }
@Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); JsonWriter writer = JsonWriter.of(buffer); adapter.toJson(writer, value); return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); } }
static Request buildRequest(HttpHeaders headers, byte[] content, URI uri, HttpMethod method) throws MalformedURLException { okhttp3.MediaType contentType = getContentType(headers); RequestBody body = (content.length > 0 || okhttp3.internal.http.HttpMethod.requiresRequestBody(method.name()) ? RequestBody.create(contentType, content) : null); Request.Builder builder = new Request.Builder().url(uri.toURL()).method(method.name(), body); headers.forEach((headerName, headerValues) -> { for (String headerValue : headerValues) { builder.addHeader(headerName, headerValue); } }); return builder.build(); }
@Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8); JsonWriter jsonWriter = gson.newJsonWriter(writer); adapter.write(jsonWriter, value); jsonWriter.close(); return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); } }
public RequestBody convert(T value) throws IOException { try { byte[] content = JSON.toJSONBytes(fastJsonConfig.getCharset() , value , fastJsonConfig.getSerializeConfig() , fastJsonConfig.getSerializeFilters() , fastJsonConfig.getDateFormat() , JSON.DEFAULT_GENERATE_FEATURE , fastJsonConfig.getSerializerFeatures() ); return RequestBody.create(MEDIA_TYPE, content); } catch (Exception e) { throw new IOException("Could not write JSON: " + e.getMessage(), e); } } }