Search code examples
jsonhttprequestnettyhttpcontent

Netty HttpRequest including Json


I am trying to build and HTTP request to our Rackspace CDN

I need to add a Json string as content to give the login instructions

I used curl to test the info and it works but through Netty I get a 400 Bad Request response which tells me I am not passing the Json correctly all the example on Github are overconvoluted and the ones in stack overflow are for version 3 any help would be greatly appreciated here is my code I am using Netty 4.1.0Beta5

URI uri = new URI("https://identity.api.rackspacecloud.com");
HttpDataFactory factory = new DefaultHttpDataFactory( 
                              DefaultHttpDataFactory.MINSIZE);
HttpPostRequestEncoder bodyRequestEncoder = new 
HttpPostRequestEncoder(factory, request, false);
HttpHeaders headers = request.headers();
headers.set(HttpHeaderNames.HOST, 
"https://identity.api.rackspacecloud.com/v2.0/tokens");
            headers.set(HttpHeaderNames.CONTENT_TYPE, new   
AsciiString("application/json"));
String json = "{\"auth\":{\"RAX-KSKEY:apiKeyCredentials\":{\"username
          \":\"AAsdFrank\",\"apiKey\":\"adfadfdadsf33434fdfdfdfaf\"}}}";
ByteBuf buffer= Unpooled.copiedBuffer(json, CharsetUtil.UTF_8);
headers.set(HttpHeaderNames.CONTENT_LENGTH,  
                  String.valueOf(buffer.readableBytes()));
bodyRequestEncoder.addBodyAttribute("json", json);
request = bodyRequestEncoder.finalizeRequest();
io.netty.channel.Channel channel =  
cdnBootstrap.connect("identity.api.rackspacecloud.com", 
443).sync().channel();
channel.writeAndFlush(request);

And I get the following response which tells me I connected with the server but my request was not formed properly

STATUS: 400 Bad Request
VERSION: HTTP/1.1
HEADER: Connection = close
HEADER: Content-Length = 166
HEADER: Content-Type = text/html
HEADER: Date = Thu, 25 Jun 2015 22:10:20 GMT
HEADER: Server = nginx
CONTENT {
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>

All sugestions welcome


Solution

  • After the usual too many hours I found the solution here it is works with Netty 4.1.0.Beta5 Request composition

    String json = "{\"auth\":{\"RAX-KSKEY:apiKeyCredentials\":{\"username
    \":\"\ADDHDD",\"apiKey\":\"adfasdfasdfasdfasdfasdfsdf\"}}}";
             URI uri = new URI("https://identity.api.rackspacecloud.com/v2.0/tokens");
             DefaultFullHttpRequest request = new 
    DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,  
    uri.getRawPath());
             request.headers().set(HttpHeaderNames.CONTENT_TYPE, 
    "application/json");
             request.headers().set(HttpHeaderNames.ACCEPT_ENCODING, 
    HttpHeaderValues.GZIP);
             request.headers().set(HttpHeaderNames.HOST, 
    "identity.api.rackspacecloud.com:443");
             ByteBuf buffer = request.content().clear();
             int p0 = buffer.writerIndex();
             buffer.writeBytes(json.getBytes(CharsetUtil.UTF_8));
             int p1 = buffer.writerIndex();
             request.headers().set(HttpHeaderNames.CONTENT_LENGTH,  
    Integer.toString(p1 - p0));
             io.netty.channel.Channel channel =  
    dnBootstrap.connect("identity.api.rackspacecloud.com", 
    443).sync().channel();
            channel.writeAndFlush(request);
    

    and here is the Initializer

    ChannelPipeline pipeline = ch.pipeline();
            if (sslCtx != null) {
                pipeline.addLast("ssl", sslCtx.newHandler(ch.alloc()));
            }
            pipeline.addLast("codec", new HttpClientCodec());
            pipeline.addLast("inflater", new HttpContentDecompressor());
            pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
            pipeline.addLast("ResponseDecoder",new HttpResponseDecoder());
            pipeline.addLast("aggregator",new 
                  HttpObjectAggregator(512*1024));
            pipeline.addLast("handler", new CdnClientHandler());