atlassian-python-api icon indicating copy to clipboard operation
atlassian-python-api copied to clipboard

[Confluence] Comma separated list in "expand" parameter fails

Open pplettner opened this issue 5 years ago • 4 comments

When retrieving a page in the REST API you are able to request multiple values in the expand parameter: /rest/api/content/12345?expand=version,space,ancestors

However, when I do the same with get_page_by_id I get an error: {'statusCode': 500, 'message': "java.lang.RuntimeException: key <version%2Cspace%2Cancestors> doesn't match pattern"}

And when you debug it, it looks like it's trying to do the request with urlencoded commas which fails: /rest/api/content/12345?expand=version%2Cspace%2Cancestors

pplettner avatar Mar 13 '20 18:03 pplettner

Hi, @pplettner! What method did you use?

SLRover avatar Apr 20 '20 21:04 SLRover

Hello, I think I'm encountering the same problem. When calling Confluence.get_page_by_id(12345, expand=["body.storage", "version"]), it throws a requests.exceptions.HTTPError.

After some debugging, I found that the expand parameter is passed to the AtlassianRestAPI.request() where it is encoded using urlencode(). The symbols in the dictionary are encoded and breaks the URL. The expected URL ending is "/rest/api/content/12345?expand=body.storage,version", which does work when calling the REST API directly.


from urllib import parse

print(parse.urlencode({"expand": ["body.storage", "version"]}))
# Output: expand=%5B%27body.storage%27%2C+%27version%27%5D

print(parse.urlencode({"expand": ["body.storage,version"]}))
# Output: expand=%5B%27body.storage%2Cversion%27%5D

print(parse.urlencode({"expand": "body.storage,version"}))
# Output: expand=body.storage%2Cversion

Any workarounds for this issue besides using the REST API directly?

Thanks.

nhtsai avatar Sep 29 '22 23:09 nhtsai

@pplettner @nhtsai Hi! How about wrapping expand parameter into other encoder?

gonchik avatar Feb 24 '23 20:02 gonchik

Hey @gonchik, I haven't been following this since my initial comment, so I don't know about the other encoder.

However, I think a possible solution is to use urlencode(..., safe=",") to make sure the comma's aren't quoted in the encoding step.

https://github.com/atlassian-api/atlassian-python-api/blob/d11bbb1ae84775988da671bdb9b04d2d3ee3240a/atlassian/rest_client.py#L224-L225

Here's the an example of the change:

>>> from urllib import parse
>>> parse.urlencode({'expand': 'history,space,version'})
'expand=history%2Cspace%2Cversion'
>>> parse.urlencode({'expand': 'history,space,version'}, safe=',')
'expand=history,space,version'

It would also be good to clarify the type of expand, i.e. a string expand = "history,space,version" or a list of strings expand = ["history", "space", "version"].

https://github.com/atlassian-api/atlassian-python-api/blob/d11bbb1ae84775988da671bdb9b04d2d3ee3240a/atlassian/confluence.py#L317

Hope this helps!

nhtsai avatar Mar 02 '23 06:03 nhtsai