cancel
Showing results for 
Search instead for 
Did you mean: 

API from CURL vs from Python

Arnault_
Confirmed Champ
Confirmed Champ

Bonjour

J'ai pu créer un nouvel utilisateur en utilisant CURL:

set header="Content-Type: application/json"
set auth="Administrator:Administrator"
set u="xxx"
set url="http://localhost:8081/nuxeo/api/v1/user"
set data="{ \"entity-type\": \"user\", \"id\":\"%u%\", \"properties\":{\"username\":\"%u%\", \"email\":\"%u%@a.b\", \"lastName\":\"%u%\", \"firstName\":\"x\", \"password\":\"%x%\" } }"

curl --trace-ascii log.txt -X POST -H %header% -u %auth% -d %data% %url%

set url="http://localhost:8081/nuxeo/api/v1/user/%u%"
curl --trace-ascii log.txt -X POST -H %header% -u %auth% %url%

et voici le résultat de la dernière commande qui confirme la création:

{"entity-type":"user","id":"xxx","properties":{"lastName":"xxx","username":"xxx","email":"xxx@a.b","company":null,"first
Name":"x","password":"","groups":[]},"extendedGroups":[{"name":"members","label":"Members group","url":"group/members"}]
,"isAdministrator":false,"isAnonymous":false}

Maintenant j'essaie de reproduire la même séquence en Python:

import urllib
import urllib2, base64, time, os
import logging as log
import json
import mimetypes, random

if __name__ == '__main__':

    url="http://localhost:8081/nuxeo/api/v1/user"

    login = "Administrator"
    passwd = "Administrator"
    auth = 'Basic %s' % base64.b64encode(login + ":" + passwd).strip()

    cookie_processor = urllib2.HTTPCookieProcessor()
    opener = urllib2.build_opener(cookie_processor)

    headers = {
	"Content-Type": "application/json",
	"Authorization": auth,
	"X-NXDocumentProperties": "*",
        }

    d = {}

    user = u'xxx'

    d['entity-type'] = 'user'
    d['id'] = user
    d['properties'] = {}
    d['properties']['username'] = user
    d['properties']['email'] = '%s@a.b' % user
    d['properties']['lastName'] = user
    d['properties']['firstName'] = 'x'
    d['properties']['password'] = ''

    data = json.dumps(d)
    data = data.strip ()

    req = urllib2.Request (url, headers=headers, data=data)

    resp = opener.open(req)
    print resp.read ()

malheureusement, ceci produit une erreur:

Caused by: java.lang.NullPointerException
        at org.nuxeo.ecm.restapi.jaxrs.io.usermanager.NuxeoPrincipalReader.readProperties(NuxeoPrincipalReader.java:179)

        at org.nuxeo.ecm.restapi.jaxrs.io.usermanager.NuxeoPrincipalReader.readJson(NuxeoPrincipalReader.java:148)
        at org.nuxeo.ecm.restapi.jaxrs.io.usermanager.NuxeoPrincipalReader.readRequest(NuxeoPrincipalReader.java:100)
        at org.nuxeo.ecm.restapi.jaxrs.io.usermanager.NuxeoPrincipalReader.readFrom(NuxeoPrincipalReader.java:84)
        at org.nuxeo.ecm.restapi.jaxrs.io.usermanager.NuxeoPrincipalReader.readFrom(NuxeoPrincipalReader.java:57)
        at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:488)
        at com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$EntityInjectable.getValue(Entity
ParamDispatchProvider.java:123)

qui montre que la lecture des properties en JSON est en erreur.

Qui pourrait m'aider?

Merci d'avance Christian

1 REPLY 1

Arnault_
Confirmed Champ
Confirmed Champ

...

OK J'ai trouvé le problème:

Pour construire les data en utilisant json.dumps, on passe un dictionnaire des propriétés. Mais on DOIT ordonner le dictionnaire, sinon, l'ordre est incorrect. En particulier, la propriété "entity-type" doit être en tête

donc on doit utiliser l'option sort_keys de la fonction dumps ainsi:

data = json.dumps(d, sort_keys=True)

J'ai proposé un PullRequest pour améliorer le package nuxeolib-py

https://github.com/nuxeo/nuxeolib-py

Cordialement Christian

Getting started

Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.