0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user7179690
  • 1,051
  • 3
  • 17
  • 40
  • *but when I use this code I got error from the server as a result* What is the error message? – Jens Sep 06 '18 at 06:48
  • Do not add the Parameter to the URL. That must be part of the request body – Jens Sep 06 '18 at 06:49
  • The documentation of curl should mention all the command line arguments and explain them. Same counts for the `python` command. If you type `man curl`, perhaps some help text shows up. – MC Emperor Sep 06 '18 at 06:51
  • without the | python part the command line work in curl so how can i represent the --form part in my java code please any help in this part how can i add it in java and --form with a binary data like the wav sound i should upload – user7179690 Sep 06 '18 at 07:02
  • Error message i got {"status":"error","short_message":"error_internal","detail_message":"An internal server error occurred."} – user7179690 Sep 06 '18 at 07:02
  • @Jens thank you bro i checked the duplicated question and you are right and the code worked Thank you for helping me again :) – user7179690 Sep 06 '18 at 07:21

0 Answers0