"Unexpected character" JSON error when creating a person using curl

Hi,

When i run this simple curl command to create a person in OpenMRS i get an error:

Command:

echo '{"gender":"M","names": [{"givenName":"John","familyName":"Smith"}]}' | curl i -u Admin:openmrs04 -H "Content-type: application/json" -X POST -d @- http://localhost:8080/openmrs/ws/rest/v1/person

Error:

curl: (6) Could not resolve host: i {“error”:{“message”:"Could not read JSON: Unexpected character (’’’ (code 39)): expected a valid value (number, String, array, object, ‘true’, ‘false’ or ‘null’ )\n at [Source: org.apache.catalina.connector.CoyoteInputStream@14100f7; …

What is it that i’m doing wrong?

Have you tried the resolution suggested here? Basically this:

echo "{\"gender\":\"M\",\"names\": [{\"givenName\":\"John\",\"familyName\":\"Smith\"}]}" | curl i -u Admin:openmrs04 -H "Content-type: application/json" -X POST -d @- http://localhost:8080/openmrs/ws/rest/v1/person

I just did but a get a different error now:

CALL:

C:\curl>echo “{“gender”:“M”,“names”: [{“givenName”:“John”,“familyNam e”:“Smith”}]}” | curl -i -u Admin:openmrs04 -H “Content-type: applicat ion/json” -X POST -d @- http://localhost:8080/openmrs/ws/rest/v1/person HTTP/1.1 500 Internal Server Error Server: Apache-Coyote/1.1 Set-Cookie: JSESSIONID=5425B023A6B7C8C39FE040C776406046; Path=/openmrs3/ Content-Type: application/json;charset=UTF-8 Content-Length: 7722 Date: Wed, 03 Jun 2015 11:05:21 GMT Connection: close

ERROR:

{"error":{"message":"Can not deserialize instance of org.openmrs.module.webservi
ces.rest.SimpleObject out of VALUE_STRING token\n at [Source: org.apache.catalin
a.connector.CoyoteInputStream@9c7963; ...

Are you sure you’re using the correct endpoint and payload? Which documentation are you reading? If you’re just trying to add a name to a person then you have to specify the person UUID like it says here.

I’m not trying to add a name to a person i’m trying to a create a new person in the openmrs database through curl.

I’s actually very simple when using the interface openmrs provides after i added the rest web services module:

TYPE: POST URI: /openmrs/ws/rest/v1/person Body content: {“gender”: “M”, “names”: [{“givenName”:“John”, “familyName”:“Smith”}]}

But then i wanted to make the same call but with curl. This is the documentation that i’ve used and looked at some curl examples for guidance.

The OpenMRS log should show the full stack trace, can you get it?

Also check that gender in fact expects an M. The documentation is not very helpful about semantics.

According to this https://wiki.openmrs.org/display/docs/Create+Patient your example should work.

This is what i have got it terms of logging:

HTTP/1.1 500 Internal Server Error
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=600468B31FAABEC5F4ADFEB029457A50; Path=/openmrs/
Content-Type: application/json;charset=UTF-8
Content-Length: 7722
Date: Thu, 04 Jun 2015 06:38:42 GMT
Connection: close

{"error":{"message":"Can not deserialize instance of org.openmrs.module.webservi
ces.rest.SimpleObject out of VALUE_STRING token\n at [Source: org.apache.catalin
a.connector.CoyoteInputStream@9147c2; line: 1, column: 1]","code":"org.codehaus.
jackson.map.JsonMappingException:159","detail":"org.codehaus.jackson.map.JsonMap
pingException: Can not deserialize instance of org.openmrs.module.webservices.re
st.SimpleObject out of VALUE_STRING token\n at [Source: org.apache.catalina.conn
ector.CoyoteInputStream@9147c2; line: 1, column: 1]\r\n\tat org.codehaus.jackson
.map.JsonMappingException.from(JsonMappingException.java:159)\r\n\tat org.codeha
us.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializati
onContext.java:192)\r\n\tat org.codehaus.jackson.map.deser.MapDeserializer.deser
ialize(MapDeserializer.java:134)\r\n\tat org.codehaus.jackson.map.deser.MapDeser
ializer.deserialize(MapDeserializer.java:23)\r\n\tat org.codehaus.jackson.map.Ob
jectMapper._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(MappingJacks
onHttpMessageConverter.java:135)\r\n\tat org.springframework.http.converter.Abst
ractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)\r\n\tat org
.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessag
eConverters(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.HandlerMethodI
nvoker.resolveHandlerArguments(HandlerMethodInvoker.java:346)\r\n\tat org.spring
framework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(H
andlerMethodInvoker.java:171)\r\n\tat org.springframework.web.servlet.mvc.annota
tion.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerA
dapter.java:426)\r\n\tat org.springframework.web.servlet.mvc.annotation.Annotati
onMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)\r\n\tat o
rg.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.ja
va:790)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(Disp
atcherServlet.java:719)\r\n\tat org.springframework.web.servlet.FrameworkServlet
.processRequest(FrameworkServlet.java:644)\r\n\tat org.springframework.web.servl
et.FrameworkServlet.doPost(FrameworkServlet.java:560)\r\n\tat javax.servlet.http
.HttpServlet.service(HttpServlet.java:646)\r\n\tat javax.servlet.http.HttpServle
t.service(HttpServlet.java:727)\r\n\tat org.apache.catalina.core.ApplicationFilt
erChain.internalDoFilter(ApplicationFilterChain.java:303)\r\n\tat org.apache.cat
alina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)\r\n\
tat org.openmrs.module.web.filter.ForcePasswordChangeFilter.doFilter(ForcePasswo
rdChangeFilter.java:65)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.
internalDoFilter(ApplicationFilterChain.java:241)\r\n\tat org.apache.catalina.co
re.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)\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(OncePerReque
stFilter.java:76)\r\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilt
er(ModuleFilterChain.java:74)\r\n\tat org.openmrs.module.webservices.rest.web.fi
lter.AuthorizationFilter.doFilter(AuthorizationFilter.java:108)\r\n\tat org.open
mrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:74)\r\n\
tat org.springframework.web.filter.ShallowEtagHeaderFilter.doFilterInternal(Shal
lowEtagHeaderFilter.java:58)\r\n\tat org.springframework.web.filter.OncePerReque
stFilter.doFilter(OncePerRequestFilter.java:76)\r\n\tat org.openmrs.module.web.f
ilter.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.ja
va:241)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(Applica
tionFilterChain.java:208)\r\n\tat org.openmrs.web.filter.OpenmrsFilter.doFilterI
nternal(OpenmrsFilter.java:111)\r\n\tat org.springframework.web.filter.OncePerRe
questFilter.doFilter(OncePerRequestFilter.java:76)\r\n\tat org.apache.catalina.c
ore.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)\r\n
\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterC
hain.java:208)\r\n\tat org.springframework.orm.hibernate3.support.OpenSessionInV
iewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)\r\n\tat org.springf
ramework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)\
r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Applica
tionFilterChain.java:241)\r\n\tat org.apache.catalina.core.ApplicationFilterChai
n.doFilter(ApplicationFilterChain.java:208)\r\n\tat org.openmrs.web.filter.Start
upFilter.doFilter(StartupFilter.java:109)\r\n\tat org.apache.catalina.core.Appli
cationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)\r\n\tat org.
apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java
:208)\r\n\tat org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:1
09)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(App
licationFilterChain.java:241)\r\n\tat org.apache.catalina.core.ApplicationFilter
Chain.doFilter(ApplicationFilterChain.java:208)\r\n\tat org.openmrs.web.filter.S
tartupFilter.doFilter(StartupFilter.java:109)\r\n\tat org.apache.catalina.core.A
pplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)\r\n\tat
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.
java:208)\r\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilte
rInternal(CharacterEncodingFilter.java:88)\r\n\tat org.springframework.web.filte
r.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)\r\n\tat org.apache
.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.ja
va:241)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(Applica
tionFilterChain.java:208)\r\n\tat org.apache.catalina.core.StandardWrapperValve.
invoke(StandardWrapperValve.java:220)\r\n\tat org.apache.catalina.core.StandardC
ontextValve.invoke(StandardContextValve.java:122)\r\n\tat org.apache.catalina.au
thenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)\r\n\tat org.apa
che.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)\r\n\tat o
rg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)\r\n
\tat org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)\r
\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.j
ava:116)\r\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapt
er.java:421)\r\n\tat org.apache.coyote.http11.AbstractHttp11Processor.process(Ab
stractHttp11Processor.java:1070)\r\n\tat org.apache.coyote.AbstractProtocol$Abst
ractConnectionHandler.process(AbstractProtocol.java:611)\r\n\tat org.apache.tomc
at.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)\r\n\tat java.u
til.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)\r\n\tat java.ut
il.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)\r\n\tat org.apache.t
omcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\r\n\tat j
ava.lang.Thread.run(Unknown Source)\r\n"}}

That looks like an HTTP request. I think @lluismf is suggesting taking a look at the Tomcat log (e.g. /var/log/tomcat6/catalina.out).

Ah, I see the stack trace is actually there, but it’s not really that helpful. There may be something more in the logs.

org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of org.openmrs.module.webservices.rest.SimpleObject out of VALUE_STRING token\n at [Source: org.apache.catalina.connector.CoyoteInputStream@9147c2; line: 1, column: 1]
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:159)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:192)
at org.codehaus.jackson.map.deser.MapDeserializer.deserialize(MapDeserializer.java:134)
at org.codehaus.jackson.map.deser.MapDeserializer.deserialize(MapDeserializer.java:23)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1588)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1172)
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:135)
at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:633)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:346)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.openmrs.module.web.filter.ForcePasswordChangeFilter.doFilter(ForcePasswordChangeFilter.java:65)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:76)
at org.openmrs.web.filter.GZIPFilter.doFilterInternal(GZIPFilter.java:59)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:74)
at org.openmrs.module.webservices.rest.web.filter.AuthorizationFilter.doFilter(AuthorizationFilter.java:108)
at org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:74)
at org.springframework.web.filter.ShallowEtagHeaderFilter.doFilterInternal(ShallowEtagHeaderFilter.java:58)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:74)
at org.openmrs.module.web.filter.ModuleFilter.doFilter(ModuleFilter.java:58)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.openmrs.web.filter.OpenmrsFilter.doFilterInternal(OpenmrsFilter.java:111)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:109)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:109)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:109)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

I see it’s mentioning token\n. Do you have an explicit newline character in your payload?

Did you get an answer to this? Have the same error

Not yet Judy.

Which error are you getting @judy? The error in the title or the error @matcha got after he escaped the characters?

1 Like

Your curl doesn’t seem exactly correct, something like below should be able to work

curl -i -u username:password -H 'Content-type: application/json' @- http://localhost:8080/openmrs/ws/rest/v1/person -X POST -d '{"gender":"M","names": [{"givenName":"John","familyName":"Smith"}]}'

The json should come after -d and i should have a hyphen before it since it is an option as in -i

2 Likes