Issue with REST APIs when adding a User to a group

Hi. I am trying to add an existing user to an existing group.

[python]
import requests
data = ‘className=XWiki.XWikiGroups&property#member=XWiki.User’
headers = {‘Content-type’: ‘application/xml’,}
response = requests.post(‘http://local:8080/xwiki/rest/wikis/xwiki/spaces/XWiki/pages/MyGroupPage/objects’, headers=headers, data=data,auth=(‘admin’, ‘pass’))

But I get a 405 error: {“code”:405,“contactEmail”:null,“description”:“The method specified in the request is not allowed for the resource identified by the request URI”,“homeRef”:“/”,“reasonPhrase”:“Method Not Allowed”,“uri”}

10.4.6 405 Method Not Allowed
The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.

Help me, please. What am I doing wrong?

According to https://www.xwiki.org/xwiki/bin/view/Documentation/UserGuide/Features/XWikiRESTfulAPI#HObjectresources POST is used to create an object. In your case if the xobjects already exists, you need to use PUT and to specify in the URL its number.

I do not understand. I have to indicate the object number, but what is this number?

xwiki_number

You can create several objects in a page, so each object is indexed with a number. You can retrieve the xobject number by checking the result of a get on http://local:8080/xwiki/rest/wikis/xwiki/spaces/XWiki/pages/MyGroupPage/objects

[python]
import requests
data = ‘className=XWiki.XWikiGroups&property#member=XWiki.User’
headers = {‘Content-type’: ‘application/xml’,}
response = requests.put(‘http://local:8080/xwiki/rest/wikis/xwiki/spaces/XWiki/pages/MyGroupPage/objects/1’, headers=headers, data=data,auth=(‘admin’, ‘pass’))

The result is the same

405
{“code”:405,“contactEmail”:null,“description”:“The method specified in the request is not allowed for the resource identified by the request URI”,“homeRef”:“/”,“reasonPhrase”:“Method Not Allowed”,“uri”:“HTTP/1.1: Status Code Definitions”}

you’re now missing the classname, just before the number:

/wikis/{wikiName}/spaces/{spaceName}[/spaces/{nestedSpaceName}]*/pages/{pageName}/objects/{className}/{objectNumber}

here your URL should probably be:

http://local:8080/xwiki/rest/wikis/xwiki/spaces/XWiki/pages/MyGroupPage/objects/XWiki.XWikiGroups/1

and I also tried this option:

400
org.restlet.ext.jaxrs.internal.exceptions.ConvertRepresentationException: Could not convert the message body to a org.xwiki.rest.model.jaxb.Object
at org.restlet.ext.jaxrs.internal.exceptions.ConvertRepresentationException.object(ConvertRepresentationException.java:49)
at org.restlet.ext.jaxrs.internal.wrappers.params.EntityGetter.getValue(EntityGetter.java:107)
at org.restlet.ext.jaxrs.internal.wrappers.params.ParameterList.get(ParameterList.java:1122)
at org.restlet.ext.jaxrs.internal.wrappers.AbstractMethodWrapper.internalInvoke(AbstractMethodWrapper.java:160)
at org.restlet.ext.jaxrs.internal.wrappers.ResourceMethod.invoke(ResourceMethod.java:281)
at org.restlet.ext.jaxrs.JaxRsRestlet.invokeMethod(JaxRsRestlet.java:997)
at org.restlet.ext.jaxrs.JaxRsRestlet.handle(JaxRsRestlet.java:746)
at org.restlet.routing.Filter.doHandle(Filter.java:150)
at org.restlet.routing.Filter.handle(Filter.java:197)
at org.restlet.routing.Router.doHandle(Router.java:422)
at org.restlet.routing.Router.handle(Router.java:641)
at org.restlet.routing.Filter.doHandle(Filter.java:150)
at org.restlet.routing.Filter.handle(Filter.java:197)
at org.restlet.routing.Filter.doHandle(Filter.java:150)
at org.restlet.routing.Filter.handle(Filter.java:197)
at org.restlet.routing.Filter.doHandle(Filter.java:150)
at org.restlet.routing.Filter.handle(Filter.java:197)
at org.restlet.routing.Filter.doHandle(Filter.java:150)
at org.restlet.routing.Filter.handle(Filter.java:197)
at org.restlet.routing.Filter.doHandle(Filter.java:150)
at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:140)
at org.restlet.routing.Filter.handle(Filter.java:197)
at org.restlet.routing.Filter.doHandle(Filter.java:150)
at org.restlet.routing.Filter.handle(Filter.java:197)
at org.restlet.engine.CompositeHelper.handle(CompositeHelper.java:202)
at org.restlet.engine.application.ApplicationHelper.handle(ApplicationHelper.java:77)
at org.restlet.Application.handle(Application.java:385)
at org.restlet.routing.Filter.doHandle(Filter.java:150)
at org.restlet.routing.Filter.handle(Filter.java:197)

So here it’s because you didn’t follow what’s the documentation specifies:

  • Accepted media types:
    • application/xml (Object element)
    • application/x-www-form-urlencoded (a set of property#name=value pairs representing properties)

So in your case you should not use a content-type application/xml but application/x-www-form-urlencoded, if you use application/xml your data must be a valid XML representation.

Great, thanks a lot!
I figured out that the “number” is the index of the object in the current request. If one object is sent in a request, this number is always zero.
Final working code [python]:

import requests
data = ‘className=XWiki.XWikiGroups&property#member=XWiki.User’
headers = {‘Content-type’: ‘application/x-www-form-urlencoded’, }
response = requests.put(‘http://local:8080/xwiki/rest/wikis/xwiki/spaces/XWiki/pages/MyGroupPage/objects/XWiki.XWikiGroups/0’, headers=headers, data=data,auth=(‘admin’, ‘pass’))

print response.status_code
print response.content