Uploading spaced tags via XWiki-API

42 views Asked by At

I'm using ruby and typhoeus-gem to upload content to XWiki pages via its RESTful API. This works perfectly. But when it comes to uploading tags I'm struggling with space characters. Adding comma seperated tags via GUI like "having space characters, another tag" will result in two tags: "having space characters" and "another tag". That's what I want, but this doesn't work with the API. The above examples will result in five tags divided by spaces.

  • having
  • space
  • characters
  • another
  • tag

The API Documentation describes how to add a tab to a page. It says, that in case of "application/x-www-form-urlencoded" the field name "tag" is used. If I use this field type, only one tag can be uploaded. Repeating this will overwrite the previous tag(s). So I tried "tags" as field type and it works for uploading multiple tags. But still the spacing problem appears as described above.

Here is the ruby code I'm using:

url = mainpage_url + "/tags"
tags = "having space characters, another tag"   

# HTTP PUT request 
request = Typhoeus::Request.new(
    url,
    ssl_verifypeer: false,
    method: :put,
    userpwd: "#{username}:#{password}",
    headers: {'Content-Type'=> "application/x-www-form-urlencoded;charset=UTF-8"},
    body: {tags: tags}
)

# Handling HTTP errors
request.on_complete do |response|
    if response.success?
        #$log.info("Tags uploaded.")
    elsif response.timed_out?
        $log.error("Time out: Tags not uploaded.")
    elsif response.code == 0
        $log.fatal("Could not get http response while uploading Tags. #{response.return_message}")
    else
        $log.fatal("HTTP request failed while uploading Tags. #{response.code.to_s}")
    end
end

request.run
response = request.response
puts response.body

I already tried:

  • Replace the tag spaces with "+", "%20" and "\s".
  • Use text/plain and application/xml as media type

I think that application/xml can be the solution. I tried different xml-strings without success. Maybe you have a clue for me.

1

There are 1 answers

0
Alexander On

While I wrote my question I discovered that replacing spaces with a newline (\n) will prevent XWiki from splitting tags by their spaces. So the string has to look like:

tags = "having\nspace\ncharacters,another\ntag"

This will result as intended in:

  • having space characters
  • another tag

Correct me, but I don't think that this is the best practice. Your are really welcome if you have another solution.