Example usage for com.squareup.okhttp MultipartBuilder FORM

List of usage examples for com.squareup.okhttp MultipartBuilder FORM

Introduction

In this page you can find the example usage for com.squareup.okhttp MultipartBuilder FORM.

Prototype

MediaType FORM

To view the source code for com.squareup.okhttp MultipartBuilder FORM.

Click Source Link

Document

The media-type multipart/form-data follows the rules of all multipart MIME data streams as outlined in RFC 2046.

Usage

From source file:twitter4j.AlternativeHttpClientImpl.java

License:Apache License

private RequestBody getRequestBody(HttpRequest req) throws UnsupportedEncodingException {
    if (HttpParameter.containsFile(req.getParameters())) {
        final String boundary = "----Twitter4J-upload" + System.currentTimeMillis();
        MultipartBuilder multipartBuilder = new MultipartBuilder(boundary).type(MultipartBuilder.FORM);
        for (HttpParameter parameter : req.getParameters()) {
            if (parameter.isFile()) {
                multipartBuilder.addPart(
                        Headers.of("Content-Disposition",
                                "form-data; name=\"" + parameter.getName() + "\"; filename=\""
                                        + parameter.getFile().getName() + "\""),
                        RequestBody.create(MediaType.parse(parameter.getContentType()), parameter.getFile()));
            } else {
                multipartBuilder.addPart(
                        Headers.of("Content-Disposition", "form-data; name=\"" + parameter.getName() + "\""),
                        RequestBody.create(TEXT, parameter.getValue().getBytes("UTF-8")));
            }/*  w w  w.  java  2 s  .c o  m*/
        }
        return multipartBuilder.build();
    } else {
        return RequestBody.create(FORM_URL_ENCODED,
                HttpParameter.encodeParameters(req.getParameters()).getBytes("UTF-8"));
    }
}