cancel
Showing results for 
Search instead for 
Did you mean: 

Create user with CUrl and JSON

abarisone
Star Contributor
Star Contributor
Hi,
we are using a custom webscript similar to the /alfresco/service/api/people webscript in order to create users passing a JSON argument with CUrl:

curl -X POST -uadmin:admin http://localhost:8080/alfresco/service/api/people -H"content-type: application/json" -d@creaDOCADM.json

The problem is that if the JSON happens to be not well formed the script crashes with a stack without making us capable to catch any exception.
In fact, using the debugger, it seems that the check on the passed JSON is performed before entering the script.
Is it possible to intercept this error in order to preliminarly check the content of the passed JSON?

Regards,
Andrea
3 REPLIES 3

kaynezhang
World-Class Innovator
World-Class Innovator
Before entering javascript controller ,webscript framework will first parse request content and try to convert it to JSON using org.json.JSONObject class.
If it can't be converted to json object ,an exception with "Failed to convert request to JSON" will be thrown,and you still can get http response with exception information like below




{
    "status" :
  {
    "code" : 500,
    "name" : "Internal Error",
    "description" : "An error inside the HTTP server which prevented it from ful
filling the request."
  },

  "message" : "05050018 Failed to convert request to JSON",
  "exception" : "org.springframework.extensions.webscripts.WebScriptException -
05050018 Failed to convert request to JSON",

  "callstack" :
  [
          ""      ,"org.json.JSONException: Expected a ',' or '}' at character 6
6"
      ,"org.json.JSONTokener.syntaxError(JSONTokener.java:413)"
      ,"org.json.JSONObject.<init>(JSONObject.java:223)"
      ,"org.json.JSONObject.<init>(JSONObject.java:420)"
      ,"org.springframework.extensions.webscripts.json.JSONReader.read(JSONReade
r.java:77)"
      ,"org.springframework.extensions.webscripts.json.JSONReader.createScriptPa
rameters(JSONReader.java:93)"
      ,"org.springframework.extensions.webscripts.AbstractWebScript.createScript
Parameters(AbstractWebScript.java:725)"
      ,"org.springframework.extensions.webscripts.DeclarativeWebScript.execute(D
eclarativeWebScript.java:81)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer$3.execute(RepositoryCo
ntainer.java:433)"
      ,"org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(
RetryingTransactionHelper.java:454)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(R
epositoryContainer.java:495)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs
(RepositoryContainer.java:553)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(Reposito
ryContainer.java:343)"
      ,"org.springframework.extensions.webscripts.AbstractRuntime.executeScript(
AbstractRuntime.java:378)"
      ,"org.springframework.extensions.webscripts.AbstractRuntime.executeScript(
AbstractRuntime.java:209)"
      ,"org.springframework.extensions.webscripts.servlet.WebScriptServlet.servi
ce(WebScriptServlet.java:132)"
      ,"javax.servlet.http.HttpServlet.service(HttpServlet.java:728)"
      ,"org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Applica
tionFilterChain.java:305)"
      ,"org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilt
erChain.java:210)"
      ,"org.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLoc
alizationFilter.java:61)"
      ,"org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Applica
tionFilterChain.java:243)"
      ,"org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilt
erChain.java:210)"
      ,"org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValv
e.java:222)"
      ,"org.apache.catalina.core.StandardContextValve.invoke(StandardContextValv
e.java:123)"
      ,"org.apache.catalina.authenticator.AuthenticatorBase.invoke(Authenticator
Base.java:502)"
      ,"org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java
:171)"
      ,"org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java
:99)"
      ,"org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953
)"
      ,"org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.
java:118)"
      ,"org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:4
08)"
      ,"org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11P
rocessor.java:1023)"
      ,"org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(Abs
tractProtocol.java:589)"
      ,"org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.j
ava:310)"
      ,"java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.jav
a:1145)"
      ,"java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.ja
va:615)"
      ,"java.lang.Thread.run(Thread.java:724)"
      ,"org.springframework.extensions.webscripts.WebScriptException: 05050018 F
ailed to convert request to JSON"
      ,"org.springframework.extensions.webscripts.json.JSONReader.read(JSONReade
r.java:82)"

  ],

  "server" : "Community v4.3.0 (r70508) schema 7,003",
  "time" : "2014-6-5 23:53:51"
}

Hi,
for me it is still unclear how can I intercept this exception since all I have is my post webscript.
Do you mean I should put this code between a try/catch clause?
How did you managed to get the exception you're showing in the post?

Regards,
Andrea

kaynezhang
World-Class Innovator
World-Class Innovator
It is just common response ,only it's content is filled with integer status code and exception detail information instead of node data.
The response body is llegal json format ,you can parse the json string and get integer status code,if it is other than 200,then something wrong happens.