I was using an API in which I want to use it in my java project they give an example in curl when I test it, it worked fine with no problem and return the result.
The question is how can I convert this code to java
this the command I have in cUrl.
curl --form text='apple' --form user_audio_file=@D:/apple.wav "https://api.speechace.co/api/scoring/text/v0.1/json?key=mykey&dialect=en-us" | python -m json.tool
my java code which i try to use.
String Url = "https://api.speechace.co/api/scoring/text/v0.1/json";
String key = "my key";
String dialect = "en-us";
String user_id = "81ozow";
Url += "?" + "key=" + key + "&dialect=" + dialect + "&user_id=" + user_id;
File file = new File("D:/apple.wav");
byte[] postData = new byte[(int) file.length()];
DataInputStream dis = new DataInputStream(new FileInputStream(file));
dis.readFully(postData);
dis.close();
String request = Url;
URL url = new URL(request);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
//conn.addRequestProperty("text", "'apple'");
//conn.addRequestProperty("user_audio_file", "D:/apple.wav");
try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
wr.write("text='apple'".getBytes());
wr.flush();
wr.write("user_audio_file=".getBytes());
wr.write(postData);
wr.flush();
} catch (Exception e) {
out.println(conn.getErrorStream().toString());
e.printStackTrace();
}
out.println("Done upload data");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
String result = "";
while ((line = rd.readLine()) != null) {
result += line;
}
rd.close();
out.println(result);
but when I use this code I got error from the server as a result, so how can I convert this cUrl code to java, I used many APIs before which give cUrl command as an example and this first time I face such kind of problem I never worked before with command like --form
and the | python -m json
how can i write them in java.
I have seen many curls command and I was able to convert it, so any help how can I do that.