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);
0

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

This answer is a bit poor, but the solution that is linked is the only one I found which works for both form data and files without any additional library.
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();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.