0

this code snippet is taken from Postman. cURL taken from the postman works fine and java code generated from postman gives a 200 response for the particular call. but the response body is not there. what should be the user agent header? Do I need to use this postman token in my java code as well? Do I need to add additional headers? My Goal is to fetch some data from this GET call.

    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .url("http://blahblah=60041441&attributes=blah,blah,blah")
            .get()
            .addHeader("User-Agent", "PostmanRuntime/7.13.0")
            .addHeader("Accept", "*/*")
            .addHeader("Cache-Control", "no-cache")
            .addHeader("Postman-Token", "7af03a15-blah,364c160f-92d7-459f-b261-4993801944a7")
            .addHeader("Host", "blahblah.na.blah.net:9081")
            .addHeader("cookie", "someURL=1800; com.ibm.isim.lastActivity=blahblahToekn; JSESSIONID=blahblahblah:1ajblahi8; LtpaToken2=blahblahbalah")
            .addHeader("accept-encoding", "gzip, deflate")
            .addHeader("Connection", "keep-alive")
            .addHeader("cache-control", "no-cache")
            .addHeader("User-Agent", "postman")
            .build();

    okhttp3.Response response= client.newCall(request).execute();
    System.out.println(response.body().toString());
senura
  • 21
  • 1
  • 5

1 Answers1

0

Suppose for simple get request following will do just fine, all other details can be omitted:

    Request request = new Request.Builder()
            .url("http://blahblah=60041441&attributes=blah,blah,blah")
            .get()
            .build();

Most of the headers (like user-agent, accept-encoding etc) will be automatically added by OkHttp client, so you can safely remove those from request:

            .addHeader("User-Agent", "PostmanRuntime/7.13.0")
            .addHeader("Host", "blahblah.na.blah.net:9081")
            .addHeader("accept-encoding", "gzip, deflate")
            .addHeader("Cache-Control", "no-cache")
            .addHeader("Connection", "keep-alive")
            .addHeader("cache-control", "no-cache")
            .addHeader("User-Agent", "postman")

Since / is a wildcard, suppose you can skip it as well.

            .addHeader("Accept", "*/*")

If you endpoint requires authentication, suppose before sending this particular Get request you need to send authentication request first. To automatically handle authentication cookies you can try to add CookieJar to your client, so those can be omitted as well (assume headers names were altered somehow, btw?):

            .addHeader("Postman-Token", "7af03a15-blah,364c160f-92d7-459f-b261-4993801944a7")
            .addHeader("cookie", "someURL=1800; com.ibm.isim.lastActivity=blahblahToekn; JSESSIONID=blahblahblah:1ajblahi8; LtpaToken2=blahblahbalah")

You can also check answers for that question about the ways to add CookieJar.