0

I intend to call a POST API, it requires a form-data as the body to be sent. I need help on how to send form-data. Following is how I am calling the api:

static URL url;
static HttpURLConnection conn;
static String response;
url = new URL("http://" + ApiConfig.URL + endpoint);
System.out.println("URL: " + url);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setConnectTimeout(3000);
conn.setReadTimeout(5000);

2 Answers2

0

You can watch the example of the Oracle website: Reading from and Writing to a URLConnection.

You can use the following code:

URLConnection connection = url.openConnection();
connection.setDoOutput(true);

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("string=" + stringToReverse); // Write you body here
out.close();
Awais Iqbal
  • 31
  • 10
0

Here is a link that shows how to send form data including files with HttpURLConnection .