Code Monkey home page Code Monkey logo

jnhttp-multipartformdata-bodypublisher's Introduction

java.net.HttpClient multipart/form-data BodyPublisher

codecov

API Document

Dependency

  <groupId>io.github.yskszk63</groupId>
  <artifactId>jnhttp-multipartformdata-bodypublisher</artifactId>
  <version>0.0.1</version>

or copy from MultipartFormDataBodyPublisher.java.

Example

        var body = new MultipartFormData()
            .add(StringPart("name", "Hello,")
            .add(StringPart("value", "World!")
            .addFile("f", Path.of("index.html"), "text/html")
            .addFile("cpuinfo", Path.of("/proc/cpuinfo"), "text/html");

        var client = HttpClient.newHttpClient();
        var request = HttpRequest.newBuilder(URI.create("http://localhost:8080/"))
            .header("Content-Type", body.contentType())
            .POST(body)
            .build();
        var response = client.send(request, BodyHandlers.ofLines());
        response.body().forEach(line -> System.out.println(line));

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

jnhttp-multipartformdata-bodypublisher's People

Contributors

yskszk63 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

maddingo kevin70

jnhttp-multipartformdata-bodypublisher's Issues

Doesn't work with SSL if using Java higher than version 11.

Hi,

I have a problem with this library. In Java 11, everything works fine. But when using Java 17 for example and posting to a HTTPS connection running apache, the file does not arrive. Nothing is posted. I found that in these java versions no Content-Length header is computed:

https://github.com/openjdk/jdk/blob/master/src/java.net.http/share/classes/jdk/internal/net/http/RequestPublishers.java

In java 11 it still computes lengths.

Do you perhaps have any idea how to work around this?

Thanks,
Merijn

bodyPublisher.contentLength() Always returns -1

    public List<TorrentContent> addTorrent(Path torrentPath) {
        try (HttpClient httpClient = HttpClient
                .newBuilder()
                .version(HttpClient.Version.HTTP_1_1)
                .build()) {
            MultipartFormDataBodyPublisher bodyPublisher = new MultipartFormDataBodyPublisher();
            bodyPublisher.addFile("torrents",torrentPath);
//            bodyPublisher.addStream("torrents", "a.torrent", () -> stream, "application/octet-stream");
            String uri = "http://127.0.0.1/1.php";//qbBaseUrl + "/api/v2/torrents/add";
            System.out.println("length: " + bodyPublisher.contentLength()  + " uri : " + uri + " type:" + bodyPublisher.contentType());
            HttpRequest httpRequest = HttpRequest.newBuilder()
                    .uri(URI.create(uri))
                    .expectContinue(true)
                    .version(HttpClient.Version.HTTP_1_1)
                    .method("POST",bodyPublisher)
                    .build();
            HttpResponse<String> response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
            System.out.println("body: " + response.body());
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException(e);
        }

        return null;
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.