akka-http: json entity response with headers

1,090 views
Skip to first unread message

ash.ta

unread,
Jan 6, 2016, 4:11:40 PM1/6/16
to Akka User List
hi hakkers,

i asked this as a part of a discussion in other thread but got no answer, so i'm trying my luck here. 

i use akka-http with java api. i need to return a json entity in httpresponse with response headers. i was advised to do something like this:

return ctx.complete(
 
HttpResponse.create()
   
.withEntity(HttpEntities.create("example"))
   
.addHeader(Location.create("http://akka.io")));

my problem is that i don't know how to send a json entity. the docs show a way to use marshallers which doesn't involve httpresponse object, which unfortunately doesn't answer my needs:

ctx.completeAs(Jackson.json(), object)

i tried a workaround 

response.withEntity( json_string ).addHeader( new RawHeader( "Content-Type", "application/json" ) )

but this doesn't work - the response arrives with content-type text/plain and i see following warn in logs: Explicitly set HTTP header 'Content-Type: application/json' is ignored, illegal RawHeader.

any ideas?
thanks!




ash.ta

unread,
Jan 7, 2016, 4:00:39 AM1/7/16
to Akka User List
ok, i found a solution 

response.withEntity( MediaTypes.APPLICATION_JSON.toContentType(), data)

does the job
Reply all
Reply to author
Forward
0 new messages