Search code examples
spring-bootapidhl

How to get response from DHL Api to browser


i have problem with api from dhl, i create GET api from dhl, when print in console, result will print, but when using browser i got response like this :

com.squareup.okhttp.internal.http.RealResponseBody@68bd3d26

this my code :

@RequestMapping("/getData")
public String getAcc() throws IOException
{


        OkHttpClient client = new OkHttpClient();
        MediaType mediaType = MediaType.parse("application/json");

        HttpUrl httpUrl = new HttpUrl.Builder()
                .scheme("https")
                .host("api-eu.dhl.com")
                .addPathSegment("track")
                .addPathSegment("shipments")
                .addQueryParameter("trackingNumber", "cencored")
                .addQueryParameter("service", "express")
                .build();

        Request request = new Request.Builder()
                .addHeader("content-type", "application/json")
                .addHeader("Connection", "close")
                .addHeader("DHL-API-Key", "cencored")
                .addHeader("ConsumerKey", "cencored")
                .addHeader("ConsumerSecret", "cencored")
                .removeHeader("Content-Encoding")
                .removeHeader("Content-Length")
                .url(httpUrl) // <- Finally put httpUrl in here
                .build();
      response = client.newCall(request).execute();
        System.out.println(response.body().string());
    return this.response.body().toString();
}

Solution

  • solved... this is weird, but work for me. so we can't call "response.body().string();" twice.