[Confluence] Comma separated list in "expand" parameter fails
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
Hi, @pplettner! What method did you use?
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.
@pplettner @nhtsai Hi! How about wrapping expand parameter into other encoder?
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!