1

The URI myresourceUrl when hit in browser shows the json content in browser.

Requirement: Need to use the get method of httpbuilder-ng to call GET of URI and response should have the content as json.

This json file will be necessary as input for another task. How to achieve this. Do we need any parser to get the returned response as json using http-builder-ng.

Expected response format: {"name":"Abc","info":{"age":45,"height":"5.5"}}

Tried the below get request using:
// setting the request URI 
    HttpBuilder http = HttpBuilder.configure(config -> {
                    config.getRequest().setUri(myresourceUrl);
                });

String response = http.get(LazyMap.class, cfg -> {
                    cfg.getRequest().getUri().setPath(myPath);
                }).toString();

Actual format we are getting: {name:Abc,info:{age:45,height:5.5}}

How to get the response indicated above in expected response format.

IKnowNothing
  • 21
  • 1
  • 2

2 Answers2

0

First, confirm that you http request is indeed returning a JSON response. If so, you can use the gson library. Try

import com.google.code.gson;
String response = gson.toJSON(http.get(LazyMap.class, cfg -> {
                cfg.getRequest().getUri().setPath(myPath);
            }));
Adnan S
  • 1,852
  • 1
  • 14
  • 19
  • It gives the error 'Type mismatch: cannot convert from LazyMap to String' – IKnowNothing Feb 11 '19 at 08:54
  • See updated code below. What value of response do you get if you try inspecting it or printing to console? – Adnan S Feb 11 '19 at 21:11
  • The code is being written in java. So i can't declare the response variable as var. – IKnowNothing Feb 12 '19 at 03:19
  • Ok, one more suggestion before I give up - see updated answer on using gson library. If the above code does not work, try reading up about the gson library which is a great tool for working with JSON in java. – Adnan S Feb 12 '19 at 03:56
0

By default a content-type of "application/json" will be parsed rather than returned as a String. You need to define a custom parser for the content-type. I put together an example using a fake server that returns "application/json" content and then shows how to return it as a string in HttpBuilder-NG:

import com.stehno.ersatz.ErsatzServer;
import groovyx.net.http.HttpBuilder;

import static com.stehno.ersatz.ContentType.APPLICATION_JSON;
import static groovyx.net.http.NativeHandlers.Parsers.textToString;

public class Runner {

    public static void main(final String[] args) {
        final ErsatzServer server = new ErsatzServer();

        server.expectations(expects -> {
            expects.get("/something").responder(response -> {
                response.body("{\"name\":\"Abc\",\"info\":{\"age\":45,\"height\":\"5.5\"}}", APPLICATION_JSON);
            });
        });

        final String response = HttpBuilder.configure(cfg -> {
            cfg.getRequest().setUri(server.getHttpUrl());
            cfg.getResponse().parser("application/json", (chainedHttpConfig, fromServer) -> textToString(chainedHttpConfig, fromServer));
        }).get(String.class, cfg -> {
            cfg.getRequest().getUri().setPath("/something");
        });

        System.out.println(response);
        System.exit(0);
    }
}

Note the cfg.getResponse().parser("application/json", (chainedHttpConfig, fromServer) -> textToString(chainedHttpConfig, fromServer)); line which is where the parsing happens (overrides the default behavior) - see also the import import static groovyx.net.http.NativeHandlers.Parsers.textToString;.

cjstehno
  • 13,468
  • 4
  • 44
  • 56