Harish Pal
Harish Pal

Reputation: 1

Error: Unable to parse JSON, Kotlin REST Json Payload contains escaped double quotes

I am facing one issue. The application is Kotlin + Java. I am calling a Kotlin REST Resource passing the escaped backslash in the payload (This request is coming from external client). but I am getting the below error: Unable to Parse JSON.

Request Payload:

{
    "eventTypeId": "USR-05 \" "
}

REST Controller

@Produces(MediaType.APPLICATION_JSON)
@Path("/audit/api/1/events")
open class WriteAuditEventResource {
    
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    fun writeEvent(eventRequest: AuditEventRequest): Response {
        return Response.status(Response.Status.CREATED).build()
    }

}

Problem:

  1. In HTTP Logs I can see that the escaped backslash is converted to double backslash (making the JSON invalid). Below are the logs:
{
    "eventTypeId": "USR-05 \\" "
}
[o.e.jetty.http.HttpParser     ] - parseNext s=CONTENT HeapByteBuffer@64870bee[p=273,l=308,c=8192,r=35]={POST /aud... 35\r\n\r\n<<<{\n    "eventTypeId": "USR-05 \\" "\n}>>>\x00\x00\x00\x00\x00\x00\x00\x00\x00...\x00\x00\x00\x00\x00\x00\x00} 
[o.e.jetty.server.HttpChannel  ] - onContent HttpChannelOverHttp@f084976{s=HttpChannelState@4962b62c{s=HANDLING rs=BLOCKING os=OPEN is=IDLE awp=false se=false i=true al=1},r=1,c=false/false,a=HANDLING,uri=//localhost:8686/audit/api/1/events/,age=35} Content@2bcdd012{HeapByteBufferR@793bf03f[p=273,l=308,c=8192,r=35]={POST /aud... 35\r\n\r\n<<<{\n    "eventTypeId": "USR-05 \\" "\n}>>>\x00\x00\x00\x00\x00\x00\x00\x00\x00...\x00\x00\x00\x00\x00\x00\x00}} 
[o.e.jetty.server.HttpInput    ] - HttpInputOverHTTP@3716942e[c=0,q=0,[0]=null,s=STREAM] addContent Content@2bcdd012{HeapByteBufferR@793bf03f[p=273,l=308,c=8192,r=35]={POST /aud... 35\r\n\r\n<<<{\n    "eventTypeId": "USR-05 \\" "\n}>>>\x00\x00\x00\x00\x00\x00\x00\x00\x00...\x00\x00\x00\x00\x00\x00\x00}} 
[o.e.jetty.http.HttpParser     ] - CONTENT --> CONTENT_END 
[o.e.jetty.server.HttpChannel  ] - onContentComplete HttpChannelOverHttp@f084976{s=HttpChannelState@4962b62c{s=HANDLING rs=BLOCKING os=OPEN is=IDLE awp=false se=false i=true al=1},r=1,c=false/false,a=HANDLING,uri=//localhost:8686/audit/api/1/events/,age=36} 
[o.e.jetty.http.HttpParser     ] - CONTENT_END --> END 
  1. In Java Servlet Filter, if I extract the request payload from HttpServlet then I am getting below string as input:
{
    "eventTypeId": "USR-05 " "
}

StackTrace:

[.JsonProcessingExceptionMapper] - Unable to process JSON ! com.fasterxml.jackson.databind.JsonMappingException: Unexpected character ('"' (code 34)): was expecting comma to separate Object entries
!  at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: *, column: ***] (through reference chain: com.anaplan.audit.write.api.request.AuditEventRequest["eventTypeId"])
! at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:402)
! at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:361)
! at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.wrapAndThrow(BeanDeserializerBase.java:1826)
! at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:280)
! at com.fasterxml.jackson.module.afterburner.deser.SuperSonicBeanDeserializer.deserialize(SuperSonicBeanDeserializer.java:155)
! Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('"' (code 34)): was expecting comma to separate Object entries
!  at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 395]
! at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:2418)
! at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:749)
! at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:673)
! at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextFieldName(UTF8StreamJsonParser.java:1061)
! at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:608)

More Details:

Kotlin Version: 1.3.0
JDK version: 1.8
jackson-module-kotlin: 2.8.9
jackson: 2.14.1
jackson-databind: 2.14.1
jackson-core: 2.14.1
jackson-annotations: 2.14.1
jackson-module-jsonSchema: 2.14.1
jackson-module-kotlin: 2.8.9

dropwizard-jetty: 2.1.10
jetty-server: 9.4.53.v20231009
jetty-servlets: 9.4.53.v20231009

dropwizard-jersey: 2.1.10
jersey-client: 2.26
jersey-media-multipart: 2.26

I tried capture the request payload in http trace and on filter (before contoller layer), mentioned above.

Upvotes: 0

Views: 30

Answers (0)

Related Questions