user3151878
user3151878

Reputation: 49

Invoking Rest API using java client passing json object

I am new to JSON. I am invoking a public rest API https://api.gdc.cancer.gov/cases

I want to query all the cases for a particular disease type( for example TCGA-LAML mentioned below).

in SOAP Ui when I POST below request in JSON format .It gives me perfect answer { "filters": {"op":"in", "content":{ "field":"cases.project.project_id", "value":["TCGA-LAML"] } } }

But I have to call POST through a java client. Even after Trying hard I am not able to set the input parameters correctly.

I am posting my code here. Can you please help me correcting the code.

package downloadtoolproject; 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import java.net.HttpURLConnection;
import java.net.URL;

public class Newtest {
    public static String sendPostRequest(String requestUrl, String payload) {
        StringBuffer jsonString = new StringBuffer();
        try {
            URL url = new URL(requestUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Accept", "application/json");
            connection.setRequestProperty("Content-Type", "application/json");
            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
            writer.write(payload);
            writer.close();
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            String line;
            while ((line = br.readLine()) != null)
            {
                jsonString.append(line);
                System.out.println(line);
            }
            br.close();
            connection.disconnect();
        }
        catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return jsonString.toString() ;
    }

    public static void main(String [] args)
    {
        String payload = "{\"field\":\"project_id\",\"value\":[\"TCGA-LAML\"]}";
        String requestUrl="https://api.gdc.cancer.gov/cases";
        sendPostRequest(requestUrl, payload);
    }

}

Upvotes: 1

Views: 4787

Answers (1)

Bhavya Jain
Bhavya Jain

Reputation: 621

I think the following solution should work for you

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Newtest {
    public static String sendPostRequest(String requestUrl, String payload) {
        StringBuffer jsonString = new StringBuffer();
        try {
            URL url = new URL(requestUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Accept", "application/json");
            connection.setRequestProperty("Content-Type", "application/json");
            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
            writer.write(payload);
            writer.close();
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            String line;
            while ((line = br.readLine()) != null) {
                jsonString.append(line);
                System.out.println(line);
            }
            br.close();
            connection.disconnect();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return jsonString.toString();
    }

    public static void main(String[] args) {
        List<String> values = new ArrayList<>();
        values.add("TCGA-LAML");
        String requestUrl = "https://api.gdc.cancer.gov/cases";
        sendPostRequest(requestUrl, preparePayload(values));

    }

    private static String preparePayload(List<String> values) {
        StringBuilder sb = new StringBuilder();
        for (String value : values) {
            sb.append("\"" + value + "\",");
        }
        String desiredValue = sb.toString().substring(0, sb.toString().length() - 1);
        return "{ \"filters\": {\"op\":\"in\", \"content\":{ \"field\":\"cases.project.project_id\", \"value\":[" + desiredValue + "] } } }";
    }

}

You just need to add all the input values in the values List and pass it to the preparePayload method ,it will convert it into a valid payload.

Upvotes: 1

Related Questions