0

I am trying to post images to an confluence page as attachment to a content.

Here is the function of my java application:

public void postAttachment(File f, String comment) throws IOException {

    if (!f.exists() || !f.canRead()) {
        throw new IOException("Could not access file " + f.getAbsolutePath());
    }

    final FileBody bin = new FileBody(f);
    final StringBody cmt = new StringBody(comment, ContentType.TEXT_PLAIN);
    final HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("file", bin).addPart("comment", cmt).build();

    final HttpPost post = new HttpPost(baseUrl + "/rest/api/content/" + contentid + "/child/attachment");
    post.setEntity(reqEntity);

    post.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials(props.getString("confluence_user"),props.getString("confluence_password")), "UTF-8", false));
    post.addHeader("X-Atlassian-Token:","no-check");

    System.out.println("executing request " + post.getRequestLine());
    final CloseableHttpClient httpclient = HttpClients.createDefault();


    final CloseableHttpResponse response = httpclient.execute(post);

    System.out.println(post.getRequestLine());
    System.out.println(response.toString());

    if (response.getStatusLine().getStatusCode() == 404) {
        throw new IOException("Status 404 thrown!");
    }

}

The output in the terminal is:

POST https://xxx.xxxx.de:443/rest/api/content/38262140/child/attachment 

and then

HttpResponseProxy{HTTP/1.1 404 Not Found [Server: Apache-Coyote/1.1, X-ASEN: SEN-1343236, Set-Cookie: JSESSIONID=9DF46011711C2828977E17A945D023E1; Path=/; Secure; HttpOnly, X-Seraph-LoginReason: OK, X-AUSERNAME: xxxx, X-Content-Type-Options: nosniff, Content-Type: text/plain, Transfer-Encoding: chunked, Date: Tue, 27 Sep 2016 11:20:35 GMT] ResponseEntityProxy{[Content-Type: text/plain,Chunked: true]}}

(I changed the domain name and username just for this post..)

So all seems ok. If i copy the generated POST url and do a GET in the browser i get a json snippet with an attachment list, manually uploaded before. So the POST url should be ok.

I searched across the web but i cant find where i am wrong with my code.. Any Suggestions?

4
  • From the documentation: STATUS 404: Returned if the requested content is not found, the user does not have permission to view it, or if the attachments exceeds the maximum configured attachment size. docs.atlassian.com/confluence/REST/latest/#content{id}/child/attachment-createAttachments Commented Oct 2, 2016 at 17:39
  • Thank you very much for your commment. With the same user i can create the same attachments manually in the web interface. My are about 150 kb big.. so unfortunately i dont think that this is the problem. I also used another (3rd Party) application to generate same content so i dont think its a permission or attachment size problem. Commented Oct 20, 2016 at 6:41
  • By curiosity, are you able to get the page using GET - https:// xxx.xxxx.de:443/rest/api/content/38262140 ? Commented Oct 20, 2016 at 20:36
  • Yes i get a complete response, beginning with {"id":"38262140","type":"page","status":"current","title":"Diagrams","space":.......} Commented Oct 21, 2016 at 12:34

2 Answers 2

2

Confluence is strict in "X-Atlassian-Token" header value. You have extra : in header name.

Change

 post.addHeader("X-Atlassian-Token:","no-check");

to

 post.addHeader("X-Atlassian-Token","no-check");

This will create correct header, and 404 error will go away

Sign up to request clarification or add additional context in comments.

Comments

0

I too am presently struggling to add attachments; now on to a 403 Forbidden...

I worked through my 404 finding that it was in my (bad) URL. However, your URL does appear correct. Any chance that Page ID is off or your authenticated user lacks permissions (globally, in the space, or at the page level)?

Edit: Oh! And, as mtheriault's post suggests, check the attachment size vis a vis your Confluence instance's "attachmentMaxSize".

3 Comments

Thanks for your comment, but with the same user i can create content with an proprietary 3rd party application. So i dont think that this is any permission or attachment size problem.
For what it's worth we just gave up on this. Just dumped the attachments into a central location (page). Ugly but client did not want to spend more time on it.
...and I see that I posted a comment as an answer. Doh! Newbie... or I was just not paying close attention. My bad. Kids don't do this! :-/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.