0

I have a java program that uploads a file to a document libary and i have a method that creates a folder. I want the file to be uploaded to the folder but i keeps getting uploaded outside the folder.

If i call the create folder method before the upload file method I get http 403 Forbidden error what could be the problem?

 public void createFolder(String digest) throws ClientProtocolException, IOException {
        HttpPost request = new HttpPost(
                "http://industrial-ex/sites/tifdemo/_api/Web/Folders/add('enovia/newfolder')");


        request.addHeader("Content-Type", "application/json;odata=verbose");
        request.addHeader("X-RequestDigest", digest);
        CloseableHttpResponse response = httpClient.execute(target, request, context);
        response.setHeader("Accept", "application/json;odata=verbose");
        System.out.println("STATUS CODE Post Folder: " + response.getStatusLine());

    }
  • 1
    Please show us the code so we know how you're doing it. – Jussi Palo Mar 26 '15 at 10:27
  • i have added the method that creates the folder – user3476614 Mar 26 '15 at 10:38
  • is there a header missing? – user3476614 Mar 26 '15 at 10:44
  • That ain't my account – user3574273 Mar 26 '15 at 10:48
  • It's a friend not me lol :) – user3574273 Mar 26 '15 at 10:49
1

Cannot test this, but would using CredentialsProvider help, assuming you're using NTLM authentication.

public void createFolder(String digest) throws ClientProtocolException, IOException {
    HttpPost request = new HttpPost(
          "http://industrial-ex/sites/tifdemo/_api/Web/Folders/add('enovia/newfolder')");

    request.addHeader("Content-Type", "application/json;odata=verbose");
    request.addHeader("X-RequestDigest", digest);

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY,
    new NTCredentials("username", "pwd", "", "domain"));
    List<string> authtypes = new ArrayList<string>();
    authtypes.add(AuthPolicy.NTLM);
    httpclient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF,authtypes);

    localContext.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider);
    CloseableHttpResponse response = httpclient.execute(request, localContext);

    response.setHeader("Accept", "application/json;odata=verbose");
    System.out.println("STATUS CODE Post Folder: " + response.getStatusLine());
}

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.