1

How do I make a post request to a document library using java?

I'm using the lastest version on this library http://hc.apache.org/

This is my post request right now:

itemUrl looks like this: http://industrial-ex/sites/tifdemo/enovia/FILENAME.txt

public void addItem(String itemUrl, File file) throws IOException {
    FileInputStream fileInputStream = new FileInputStream(file);
    HttpEntity test = new InputStreamEntity(fileInputStream);
    HttpPost request = new HttpPost(itemUrl);
    System.out.println("ITEM URL: " + itemUrl);
    request.addHeader("Content-Type", "application/json;odata=verbose");
    request.setEntity(test);
    CloseableHttpResponse response = httpClient.execute(target, request, context);
    response.addHeader("Content-Type", "application/json;odata=verbose");
    System.out.println("STATUS CODE POST: " + response.getStatusLine());

}

I'm getting a status code:

200 OK

but my document library is empty and nothing shows up.

0

I solved it so ill answer my own question:

 public void addItem(String itemUrl, File file) throws IOException {
    TempFiles tf = new TempFiles();
    HttpPut request = new HttpPut(itemUrl);
    request.setEntity(new FileEntity(file));
    System.out.println("ITEM URL: " + itemUrl);
    CloseableHttpResponse response = httpClient.execute(target, request, context);
    System.out.println("STATUS CODE POST: " + response.getStatusLine());
    tf.deleteTempFiles();

}

I removed the headers and changed to httpPut instead of httpPost. I got a error code: 404 when i used httpPost, dont ask me why. Maybe someone else can answer that question here. I also used FileEntity in my request.setEntity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.