0

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();
}

2 Answers2

0

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

0

This is the correctly way to consume a soap webservice with spring boot: https://spring.io/guides/gs/consuming-web-service/

Follow this tutorial and it works fine.

  • 1
    Till Glöckner, a link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](/help/deleted-answers) – Yunnosch Jul 15 '22 at 12:34