1

i want to code a java http request for posting audio file (wav) for speech to text transformation. I´m quite new to http requests, and could not find any useful hints how to achieve that. In https://www.microsoft.com/cognitive-services/en-us/Speech-api/documentation/GetStarted/GetStarted-cURL I was able to get token (Step 1 in url), but now I am struggeling with step 2. Can someone help or even provide java code for step 2 (post wav file to cognitive services).

CURL Request given:

curl -v -X POST "https://speech.platform.bing.com/recognize? scenarios=smd&appid=D4D52672-91D7-4C74-8AD8- 42B1D98141A5&locale=your_locale&device.os=your_device_os&version=3.0&format=json&instanceid=your_instance_id&requestid=your_request_id" -H 'Authorization: Bearer your_access_token' -H 'Content-type: audio/wav; codec="audio/pcm"; samplerate=16000' --data-binary @your_wave_file

My Java HTTP code so far (Using Apache):

    HttpClient client = HttpClientBuilder.create().build();
    String url = "https://speech.platform.bing.com/recognize";

    String appId = "D4D52672-91D7-4C74-8AD8-42B1D98141A5"; //Always use this. See Docu
    String token = "12345"; // received from step 1 (see MS documentation)
    String locale = "de-DE";
    String deviceOS = "Windows";
    String version = "3.0"; 
    String format = "json"; 
    String instanceid = UUID.randomUUID().toString();
    String scenarios = "smd";

    // setting up post parameters
    Map<String, String> postParameters = new HashMap<>();
    postParameters.put("scenarios", scenarios);
    postParameters.put("appid", appId);
    postParameters.put("locale", locale);
    postParameters.put("device.os", deviceOS);

    postParameters.put("format", format);
    postParameters.put("instanceid", instanceid);
    postParameters.put("requestid", instanceid);
    postParameters.put("version", version);

    // setting up HttpPost
    HttpPost httpPost = new HttpPost(url);

    // PARAMETERS
    List<NameValuePair> qparams = new ArrayList<>();
    for (Map.Entry<String, String> s : postParameters.entrySet()) {
        qparams.add(new BasicNameValuePair(s.getKey(), s.getValue()));
    }
    httpPost.setEntity(new UrlEncodedFormEntity(qparams));
    // HEADERS
    String wavFile = "C:\\Folder\\AudioData.wav";

    Map<String, String> postHeaders = new HashMap<>();
    postHeaders.put("Authorization", "Bearer " + token);
    postHeaders.put("Content-Type", "audio/wav; codec=\"audio/pcm\"; samplerate=16000");


    for (Map.Entry<String, String> entry : postHeaders.entrySet()) {
        httpPost.setHeader(entry.getKey(), entry.getValue());
    }

    // WAV FILE
    File file = new File(wavFile);
    FileBody bin = new FileBody(file, ContentType.DEFAULT_BINARY);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    builder.addPart("bin", bin);

    HttpEntity reqEntity = builder.build();
    httpPost.setEntity(reqEntity);

    // RESPONSE
    HttpResponse response = client.execute(httpPost);

    int responseCode = response.getStatusLine().getStatusCode();

    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

    BufferedReader rd = new BufferedReader(
            new InputStreamReader(response.getEntity().getContent()));

    StringBuilder result = new StringBuilder();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    System.out.println(result.toString());

My Response code is 200, but my response.getEntity().getContent() is empty. I expect a JSON there.

btw: if i fire this up with curl (with set parameters of course) it works, and I get a JSON back with recognized speech to text.

Can you help me with that?

Nycon

  • 1
    You overwrote the `Content-Type` and why did you double quote `audio/pcm`? – OneCricketeer Nov 25 '16 at 23:01
  • Thanks for your reply. I've corrected it. No success. – Darth Nycon Nov 26 '16 at 21:46
  • Alright, well `scenarios` for example, is called a query parameter. It, and everything else in the URL after the question mark is not meant to be sent in the POST body. The only thing you have to add to the POST parameters should be `your_wave_file` – OneCricketeer Nov 27 '16 at 01:21

0 Answers0