Ken K
Ken K

Reputation: 77

Confluence REST API: HTTP 200 w/ Empty Response

I cannot seem to create a new page on Confluence Cloud v1000.957.0 via the Rest API. I have tried both using cURL and Python to no avail: all I get back is HTTP 200 with an empty JSON response every time. If I change the credentials to be incorrect, I get HTTP 401.

curl -u ******@******.com:****** -X POST -H 'Content-Type: application/json' -d'{"type":"page","title":"TESTING123", "ancestors":[{"id":123456}], "space":{"key":"AA"},"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}' https://********.atlassian.net/wiki/rest/api/content/

I have changed a few details of the cURL call for the sake of anonymity, but I know the credentials, ancestor ID, space key, and API URL are correct. I have also verified that this account has the correct permissions in Confluence to create a page in this space, and that API access for Confluence is enabled.

Variations on the API call that I have tried:

  1. Adding ?os_authType=basic to the end of the URL

  2. Adding as many parameters as possible (all with empty values) such as:

    • status: current
    • operations: []
    • metadata: {}
  3. Changing the API call to create a page at the top level within the Confluence space (omitting the ancestors field).

  4. Adding this header: Accept: application/json

  5. Adding the view option to the body portion of the call and subsequently appending this to the end of the URL: &expand=body.view. I have found that adding it to the Python code gives the same result, but adding it to the cURL call prints out something like [1] 42999 (the latter part changes with each call); then upon a new command or simply upon pressing Return, it prints [1]+ Done followed by a chunk of whitespace, then the cURL call I just made.

I found a very similar bug in Atlassian's JIRA for JIRA software, so apparently this happens for JIRA as well, but intermittently. https://jira.atlassian.com/browse/JRA-41559

I'm not terribly familiar with HTTP protocol, so there may be something I am overlooking, so really I just want more options to try (although a solution would be ideal, of course).

Upvotes: 3

Views: 3312

Answers (3)

rickrizzo
rickrizzo

Reputation: 602

This was driving my crazy as well. My solution was finally found by creating a session. Not sure exactly how to do this using cURL. May have to use PHP or something. I used NodeJS

What's going on here is XSRF tokens are being sent out when you make your first request. You need a session to store these. Once stored, they must be sent out with all subsequent requests.

My application was written in NodeJS. YMMV in other languages, but the following worked for me:

var request = require('request');

/* GET home page. */
router.get('/', function(req, res, next) {
  var cookieJar = request.jar(); // <-- THIS IS THE IMPORTANT PART

  var options = { method: 'GET',
  url: 'https://test.atlassian.net/wiki/rest/api/content',
  jar: cookieJar,
  headers:
   {'cache-control': 'no-cache',
     authorization: 'Basic yourBase64AuthToken' } };

  // You'll need to make two requests, one to get the tokens,
  // and another to actually get your query to work
  request(options, function (error, response, body) {
    if (error) throw new Error(error);
    request(options, function (error, response, body) {
      if (error) throw new Error(error);
      console.log(body);
    });
  });

res.send('hello');
});

Upvotes: 0

Patrick
Patrick

Reputation: 2247

For anyone else who ends up here... I had this exact problem where a r = requests.get(URL, auth=(USER, PASSWORD) to a URL that was a wiki page .../wiki/rest/api/content/123454 would have a 200 r.status_code and r.text was empty.

Fix is to use requests.session() and make two calls. One (I assume) to establish the session and the second to get the content.

s = requests.session()
s.auth = (USER, PASSWORD)
r = s.get(url_base) # url_base = '.../wiki/rest/api'
r = s.get(url) # url = '.../wiki/rest/api/content/123453'

then r.text will have content.

Upvotes: 1

Maciej
Maciej

Reputation: 56

The problem is that you're using username with domain as credentials (******@******.com:******)

You can check if this is working just by asking for main wiki content.
Try: curl -u <user>:<pass> https://<domain>.atlassian.net/wiki/rest/api/content/

You can also ommit password. Curl will ask for it - it's safer from security perspective.
Try: curl -u <user> https://<domain>.atlassian.net/wiki/rest/api/content/

Upvotes: 4

Related Questions