我正在尝试在scala中使用netty.IO获取http响应(json)的主体(阻塞,而不是异步)。
我找到的大多数示例和东西都是Java,我似乎找不到一种方法来“只获取响应的http正文”。我总是以响应代码或不是主体的对象的.toString结束。
下面是我到目前为止所掌握的
def getRestContentFromNetty(url:String) : String ={
// IO.netty!
val response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
response.headers().set(Names.CONTENT_TYPE, accept_val).set(Names.AUTHORIZATION, basic_auth)
val bbuf = Unpooled.copiedBuffer("{\"jsonrpc\":\"2.0\",\"method\":\"calc.add\",\"params\":[1,2],\"id\":1}", StandardCharsets.UTF_8);
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, bbuf.readableBytes());
val q = response.content().clear().writeBytes(bbuf);
logInfo(response.getStatus.toString)// -> this logs "200 OK", so the response seems good.
q.toString
}这将返回"UnpooledHeapByteBuf(ridx: 0,widx: 59,cap: 64)“,而不是正文
我在这里遗漏的主要概念是什么?
发布于 2016-06-14 18:21:16
如果要将ByteBuf打印为字符串,可以使用:
q.toString(Charset)
https://stackoverflow.com/questions/37698000
复制相似问题