Search code examples
javarestjax-rsresteasyput

Put request with resteasy


I want to construct a put request with resteasy that resembles the following curl command:

curl -u myUser:myP455w0rd! -X PUT "http://localhost:8081/artifactory/my-repository/my/new/artifact/directory/file.txt" -T Desktop/myNewFile.txt

I have started with something like

Client client = ClientBuilder.newClient();
ResteasyWebTarget rWebTarget = (ResteasyWebTarget) client.target(targetPath);
rWebTarget.register(new BasicAuthentication(user, password));

rWebTarget.request().put(entity);

I guess I have to specify the local file as entity but it is not clear to me how to do that.


Solution

  • It seems to work in the following way:

    Client client = ClientBuilder.newClient();
    ResteasyWebTarget rWebTarget = (ResteasyWebTarget) client.target(targetPath);
    rWebTarget.register(new BasicAuthentication(user, password));
    
    InputStream stream = Files.newInputStream(path);
    
    Entity<InputStream> entity = Entity.entity(stream, MediaType.APPLICATION_OCTET_STREAM);
    Response put = rWebTarget.request().put(entity);