Java Code Examples for java.net.http.HttpRequest#BodyPublisher

The following examples show how to use java.net.http.HttpRequest#BodyPublisher . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FormBodyPublisher.java    From Java-Coding-Problems with MIT License 6 votes vote down vote up
public static HttpRequest.BodyPublisher ofForm(Map<Object, Object> data) {

        StringBuilder body = new StringBuilder();

        for (Object dataKey : data.keySet()) {

            if (body.length() > 0) {
                body.append("&");
            }

            body.append(encode(dataKey))
                    .append("=")
                    .append(encode(data.get(dataKey)));
        }

        return HttpRequest.BodyPublishers.ofString(body.toString());
    }
 
Example 2
Source File: HttpChannel.java    From Fibry with MIT License 5 votes vote down vote up
private static HttpRequest.BodyPublisher dataAsPublisher(String remoteActorNameEncoded, String objectTypeEncoded, String messageEncoded) {
    var builder = new StringBuilder();

    builder.append("actorName=");
    builder.append(remoteActorNameEncoded);
    builder.append("&type=");
    builder.append(objectTypeEncoded);
    builder.append("&message=");
    builder.append(messageEncoded);

    return HttpRequest.BodyPublishers.ofString(builder.toString());
}
 
Example 3
Source File: Main.java    From keycloak-extension-playground with Apache License 2.0 5 votes vote down vote up
public static HttpRequest.BodyPublisher ofFormData(Map<Object, Object> data) {
    var builder = new StringBuilder();
    for (Map.Entry<Object, Object> entry : data.entrySet()) {
        if (builder.length() > 0) {
            builder.append("&");
        }
        builder.append(URLEncoder.encode(entry.getKey().toString(), StandardCharsets.UTF_8));
        builder.append("=");
        builder.append(URLEncoder.encode(entry.getValue().toString(), StandardCharsets.UTF_8));
    }
    return HttpRequest.BodyPublishers.ofString(builder.toString());
}
 
Example 4
Source File: MultipartBodyPublisher.java    From Java-Coding-Problems with MIT License 5 votes vote down vote up
public static HttpRequest.BodyPublisher ofMultipart(
        Map<Object, Object> data, String boundary) throws IOException {

    final byte[] separator = ("--" + boundary + LINE_SEPARATOR
            + "Content-Disposition: form-data; name=").getBytes(StandardCharsets.UTF_8);
    final List<byte[]> body = new ArrayList<>();

    for (Object dataKey : data.keySet()) {
        body.add(separator);

        Object dataValue = data.get(dataKey);

        if (dataValue instanceof Path) {
            Path path = (Path) dataValue;

            String mimeType = fetchMimeType(path);

            body.add(("\"" + dataKey + "\"; filename=\"" + path.getFileName()
                    + "\"" + LINE_SEPARATOR + "Content-Type: "
                    + mimeType + LINE_SEPARATOR + LINE_SEPARATOR)
                    .getBytes(StandardCharsets.UTF_8));

            body.add(Files.readAllBytes(path));

            body.add(LINE_SEPARATOR.getBytes(StandardCharsets.UTF_8));
        } else {
            body.add(("\"" + dataKey + "\""
                    + LINE_SEPARATOR + LINE_SEPARATOR + dataValue + LINE_SEPARATOR)
                    .getBytes(StandardCharsets.UTF_8));
        }
    }

    body.add(("--" + boundary + "--").getBytes(StandardCharsets.UTF_8));

    return HttpRequest.BodyPublishers.ofByteArrays(body);
}
 
Example 5
Source File: DiscordManager.java    From BHBot with GNU General Public License v3.0 5 votes vote down vote up
public HttpRequest.BodyPublisher build() {
    if (partsSpecificationList.size() == 0) {
        throw new IllegalStateException("Must have at least one part to build multipart message.");
    }
    addFinalBoundaryPart();
    return HttpRequest.BodyPublishers.ofByteArrays(PartsIterator::new);
}
 
Example 6
Source File: HttpUtils.java    From alf.io with GNU General Public License v3.0 5 votes vote down vote up
public static <K, V> HttpRequest.BodyPublisher ofFormUrlEncodedBody(Map<K, V> data) {
    Objects.requireNonNull(data);
    StringBuilder sb = new StringBuilder();
    data.forEach((k,v) -> {
        if (sb.length() > 0) {
            sb.append("&");
        }
        sb.append(URLEncoder.encode(k.toString(), StandardCharsets.UTF_8)).append("=").append(URLEncoder.encode(v.toString(), StandardCharsets.UTF_8));
    });
    return HttpRequest.BodyPublishers.ofString(sb.toString());
}
 
Example 7
Source File: HttpUtils.java    From alf.io with GNU General Public License v3.0 5 votes vote down vote up
public HttpRequest.BodyPublisher build() {
    if (partsSpecificationList.size() == 0) {
        throw new IllegalStateException("Must have at least one part to build multipart message.");
    }
    addFinalBoundaryPart();
    return HttpRequest.BodyPublishers.ofByteArrays(PartsIterator::new);
}
 
Example 8
Source File: SimpleHttpClient.java    From alf.io with GNU General Public License v3.0 4 votes vote down vote up
private static HttpRequest.BodyPublisher buildRequestBody(Object body) {
    return body == null ? HttpRequest.BodyPublishers.noBody() : HttpRequest.BodyPublishers.ofString(Json.GSON.toJson(body));
}