-2

I can't find any documentation or I'm just getting it wrong while searching.

How can I call a rest api example : http://192.168.1.123:1106/idauthenticator/listoffullname

That API is accepting array of uids example: List of uids (Body request) with response entity of (firstname, lastname, uid)

How can I call that API with an ArrayList of uid so that I can get a response from that API.

BTW that api is getting all the fullname according to uids.

This is my get API of listoffullname with requestbody of list uids.

@GetMapping(path = "/listoffullname", produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<ListOfFullname> getFullname(
        @RequestHeader Map<String, String> headers,
        @RequestBody List<Uids> listofuid) throws URISyntaxException {

    ListOfFullname uids = listoffullname.getFullname(listofuid);
    return ResponseEntity.ok(uids);
}


public class FullnameRetrieverService {
public List<String> getFullname (List<String> listofuid) throws URISyntaxException {

        URIBuilder builder = new URIBuilder("http://192.168.1.100:1106/idauthenticator/listoffullname");
        String url = "http://192.168.1.100:1106/idauthenticator/listoffullname";
        RestTemplate restTemplate = new RestTemplate();

        // set headers
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // create request body
        HttpEntity<List<String>> entity = new HttpEntity<List<String>>(listofuid, headers);
        System.out.println(entity.getBody());


        ResponseEntity<ListOfFullname> test = restTemplate.exchange(builder.toString(), HttpMethod.GET, entity, ListOfFullname.class);
        System.out.println(test.getBody().getFirstname());

        return entity.getBody();

    }}
Lep
  • 21
  • 2
  • 9

2 Answers2

0

Are you looking for how to do this in Java or How to Assemble the URL?

You can assemble the URL as a string and use HTTPClient to call the endpoint.

Here is the discussion on best practice to call REST API with a list of parameters

Even though HTTP spec does not say that GET cannot have a body, most standard servers and test clients don't support it. It should ideally be avoided as most client developers would not expect a GET to have a Body.

You can try curl its fairly raw to block you from doing this.

Here another discussion on the topic of HTTP GET with body

Hope this helps :)

A G
  • 297
  • 1
  • 7
  • I can create a rest API get with request body of list and can give me an output of Json (firstname, lastname, uid). My concern is how can I include an arraylist of uid when I call the rest API that I created ? . – Lep Apr 26 '20 at 08:10
  • Instead of calling the api in Postman . I would like to call the API via java code to translate it via Java Object. – Lep Apr 26 '20 at 08:12
  • You can try the following way: host:port/app-context/customer?uids=uid1,uid2,uid3 On the controller side: You could use @RequestParams("uids") List uids (That takes care of "List" on the server side) On the client side: You could convert List to CSV String. – A G Apr 26 '20 at 21:10
  • I updated my question I included there the rest template I created I'm getting null value. Is there something I missed ? – Lep Apr 27 '20 at 04:54
0

Here is this code ! start with it :

Map.Entry<String,JsonNode> field = null;
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            HttpGet request = new HttpGet("http://192.168.1.100:1106/idauthenticator/listoffullname");
            URL url = new URL("http://192.168.1.100:1106/idauthenticator/listoffullname");
            HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
             JsonFactory factory = new JsonFactory();
    //HERE YOU PUT YOUR BODY
             String body="";
             ObjectMapper mapper = new ObjectMapper(factory);
             JsonNode rootNode = mapper.readTree(body);  

             Iterator<Map.Entry<String,JsonNode>> fieldsIterator = rootNode.fields();
          // Request parameters and other properties.
             List<NameValuePair> params = new ArrayList<NameValuePair>();
             while (fieldsIterator.hasNext()) {

                 field = fieldsIterator.next();
                 params.add(new BasicNameValuePair(field.getKey().toString(), field.getValue().toString()));
             }


             AuthCache authCache = new BasicAuthCache();
             authCache.put(target, new BasicScheme());
             HttpClientContext localContext = HttpClientContext.create();
             localContext.setAuthCache(authCache);
             try (CloseableHttpClient httpClient = HttpClientBuilder.create()
                     .build();
                  CloseableHttpResponse response = httpClient.execute(target, request, localContext)) {


          // 401 if wrong user/password
             System.out.println(response.getStatusLine().getStatusCode());

             HttpEntity entity = response.getEntity();
             if (entity != null) {
                 // return it as a String
                 String result = EntityUtils.toString(entity);
                 System.out.println(result);

             }
maryem neyli
  • 467
  • 3
  • 20