Trying to create a patient by REST module using POST

2 views
Skip to first unread message

Juan Camilo Vanegas Serna

unread,
Apr 9, 2014, 11:53:35 AM4/9/14
to d...@openmrs.org
Hi everybody. I am trying to figure out how to use correctly the REST web service module. I am working on HTML/CSS and JS to run AJAXpetition to my localhost.
I already read from the wiki page that I need to send the identifiers (as a Array) and the person (as a object). Here is how I organized my JSON:

var data ={"identifiers":[{ "identifier":"DFGHA-3",
                               "identifierType":"8d79403a-c2cc-11de-8d13-0010c6dffd0f",
                               "location":"8d79403a-c2cc-11de-8d13-0010c6dffd0f",
                             "preferred":true
                            }],
              "person":{"gender":"M",
                      "birthdate":"1990-10-29",
                      "names":[{"givenName":"Pepito",
                                       "familyName":"Perez"
                                          }]
                         }
};

And the AJAX is organized:

$.ajax({
    type: 'POST',
    data: data,
    contentType: 'application/json; charset=UTF-8',
        dataType: 'json',
     xhrFields:{
             withCredentials: true
     },
     beforeSend: function (xhr){
              xhr.setRequestHeader('Authorization','Basic '+btoa('admin'+":"+'Admin123'));            
     },
    success: function(data){
          console.log(data);
    },
    error: function(xhr, type){
       console.log('Ajax error!');
    }
});

But when I send my data, I receive: 

{"error":{"message":"Could not read JSON: Unexpected character ('i' (code 105)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.apache.catalina.connector.CoyoteInputStream@299e2af7; line: 1, column: 2]; nested exception is org.codehaus.jackson.JsonParseException: Unexpected character ('i' (code 105)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.apache.catalina.connector.CoyoteInputStream@299e2af7; line: 1, column: 2]","code":"org.springframework.http.converter.json.MappingJacksonHttpMessageConverter:138","detail":"org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unexpected character ('i' (code 105)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.apache.catalina.connector.CoyoteInputStream@299e2af7; line: 1, column: 2]; nested exception is org.codehaus.jackson.JsonParseException: Unexpected character ('i' (code 105)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.apache.catalina.connector.CoyoteInputStream@299e2af7; line: 1, column: 2]\r\n\tat org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:138)\r\n\tat org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)\r\n\tat org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:633)\r\n\tat org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:597)\r\n\tat org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:346)\r\n\tat org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)\r\n\tat org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)\r\n\tat org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)\r\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)\r\n\tat org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:637)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:717)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.module.web.filter.ForcePasswordChangeFilter.doFilter(ForcePasswordChangeFilter.java:65)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:76)\r\n\tat org.openmrs.web.filter.GZIPFilter.doFilterInternal(GZIPFilter.java:59)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)\r\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:74)\r\n\tat org.openmrs.module.webservices.rest.web.filter.AuthorizationFilter.doFilter(AuthorizationFilter.java:100)\r\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:74)\r\n\tat org.springframework.web.filter.ShallowEtagHeaderFilter.doFilterInternal(ShallowEtagHeaderFilter.java:58)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)\r\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:74)\r\n\tat org.openmrs.module.web.filter.ModuleFilter.doFilter(ModuleFilter.java:58)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.web.filter.OpenmrsFilter.doFilterInternal(OpenmrsFilter.java:111)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:109)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:109)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:109)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)\r\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)\r\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)\r\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)\r\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)\r\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)\r\n\tat org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)\r\n\tat org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)\r\n\tat org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)\r\n\tat java.lang.Thread.run(Unknown Source)\r\nCaused by: org.codehaus.jackson.JsonParseException: Unexpected character ('i' (code 105)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.apache.catalina.connector.CoyoteInputStream@299e2af7; line: 1, column: 2]\r\n\tat org.codehaus.jackson.JsonParser._constructError(JsonParser.java:943)\r\n\tat org.codehaus.jackson.impl.JsonParserBase._reportError(JsonParserBase.java:632)\r\n\tat org.codehaus.jackson.impl.JsonParserBase._reportUnexpectedChar(JsonParserBase.java:565)\r\n\tat org.codehaus.jackson.impl.Utf8StreamParser._handleUnexpectedValue(Utf8StreamParser.java:1077)\r\n\tat org.codehaus.jackson.impl.Utf8StreamParser.nextToken(Utf8StreamParser.java:212)\r\n\tat org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:1625)\r\n\tat org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1580)\r\n\tat org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1172)\r\n\tat org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:135)\r\n\t... 61 more\r\n"}}

I believe i have some problems with my data variable. The server seems to have problems when try to read the JSON, and the problem is with the 'i' from the identifiers. If i change the order, that is, person first and then identifiers, the error change to Could not read JSON: Unexpected character ('p'...

Thanks for any help can give me,
Juan Camilo.

Juan Camilo Vanegas Serna

unread,
Apr 9, 2014, 5:08:09 PM4/9/14
to d...@openmrs.org
I already find the error. The first one was the uuid for location and identifier_type. This data is: 

"identifierType":"8d793bee-c2cc-11de-8d13-0010c6dffd0f",
"location":"8d6c993e-c2cc-11de-8d13-0010c6dffd0f"

and not

"identifierType":"8d79403a-c2cc-11de-8d13-0010c6dffd0f",
"location":"8d79403a-c2cc-11de-8d13-0010c6dffd0f"

The second was for the process data of AJAX. You need to set to false. So the AJAX finally is:

$.ajax({
 type: 'POST',
 processData:false,
 data: JSON.stringify(data),
 contentType: 'application/json;charset=UTF-8',
 dataType: 'json',
     xhrFields:{
             withCredentials: true
          },
          beforeSend: function (xhr){ 
             xhr.setRequestHeader('Authorization','Basic '+btoa('admin'+":"+'Admin123'));           
          },
 success: function(data){
   console.log(data);
 },
 error: function(xhr, type){
   console.log('Ajax error!');
 }
});

I hope this can help someone.

Juan Camilo.

Lluis Martinez

unread,
Apr 9, 2014, 5:22:17 PM4/9/14
to d...@openmrs.org
But the error was highly misleading, it should have returned some "identifier type xxx is invalid" message. I think it's worth a ticket.

Cheers


--
OpenMRS Developers: http://go.openmrs.org/dev
Post: d...@openmrs.org | Unsubscribe: dev+uns...@openmrs.org
Manage your OpenMRS subscriptions at https://id.openmrs.org/

To unsubscribe from this group and stop receiving emails from it, send an email to dev+uns...@openmrs.org.

Juan Camilo Vanegas Serna

unread,
Apr 9, 2014, 5:29:08 PM4/9/14
to d...@openmrs.org
But that happens, I forgot say the error when  processData is true in AJAX  instead use JSON.stringify. This is the error when you send the JSON without stringify it:

{"error":{"message":"Can not deserialize instance of org.openmrs.module.webservices.rest.SimpleObject out of START_ARRAY token\n at [Source: org.apache.catalina.connector.CoyoteInputStream@299e2af7; line: 1, column: 1]","code":"org.codehaus.jackson.map.JsonMappingException:159","detail":"org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of org.openmrs.module.webservices.rest.SimpleObject out of START_ARRAY token\n at [Source: org.apache.catalina.connector.CoyoteInputStream@299e2af7; line: 1, column: 1]\r\n\tat org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:159)\r\n\tat org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:192)\r\n\tat org.codehaus.jackson.map.deser.MapDeserializer.deserialize(MapDeserializer.java:134)\r\n\tat org.codehaus.jackson.map.deser.MapDeserializer.deserialize(MapDeserializer.java:23)\r\n\tat org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1588)\r\n\tat org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1172)\r\n\tat org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:135)\r\n\tat org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)\r\n\tat org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:633)\r\n\tat org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:597)\r\n\tat org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:346)\r\n\tat org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)\r\n\tat org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)\r\n\tat org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)\r\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)\r\n\tat org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:637)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:717)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.module.web.filter.ForcePasswordChangeFilter.doFilter(ForcePasswordChangeFilter.java:65)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:76)\r\n\tat org.openmrs.web.filter.GZIPFilter.doFilterInternal(GZIPFilter.java:59)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)\r\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:74)\r\n\tat org.openmrs.module.webservices.rest.web.filter.AuthorizationFilter.doFilter(AuthorizationFilter.java:100)\r\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:74)\r\n\tat org.springframework.web.filter.ShallowEtagHeaderFilter.doFilterInternal(ShallowEtagHeaderFilter.java:58)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)\r\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:74)\r\n\tat org.openmrs.module.web.filter.ModuleFilter.doFilter(ModuleFilter.java:58)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.web.filter.OpenmrsFilter.doFilterInternal(OpenmrsFilter.java:111)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:109)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:109)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:109)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)\r\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)\r\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)\r\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)\r\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)\r\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)\r\n\tat org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)\r\n\tat org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)\r\n\tat org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)\r\n\tat java.lang.Thread.run(Unknown Source)\r\n"}}


Reply all
Reply to author
Forward
0 new messages