watson-developer-cloud / java-sdk

:1st_place_medal: Java SDK to use the IBM Watson services.
http://watson-developer-cloud.github.io/java-sdk/
Apache License 2.0
593 stars 532 forks source link

Error when calling synthesize method of TextToSpeech() class. Is this a bug? #124

Closed RobWills closed 9 years ago

RobWills commented 9 years ago

When I call the synthesize method of TextToSpeech() like so:

TextToSpeech tts_service = new TextToSpeech();
tts_service.setUsernameAndPassword("username_here", "password_here");
InputStream is = tts_service.synthesize("The cat sat on the mat");

I get a bad request exception:

Nov 25, 2015 4:58:55 PM com.ibm.watson.developer_cloud.service.WatsonService execute 
SEVERE: https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?text=The%20cat%20sat%20on%20the%20mat&voice=en-US_LisaVoice&Accept=audio%2Fogg%3B%20codecs%3Dopus, status: 400, error: The argument(s) [u'Accept'] are not allowed. 
Nov 25, 2015 4:58:55 PM com.vaadin.server.DefaultErrorHandler doDefault 
SEVERE: com.ibm.watson.developer_cloud.service.BadRequestException: 
The argument(s) [u'Accept'] are not allowed. at 
com.ibm.watson.developer_cloud.service.WatsonService.execute(WatsonService.java:128) 
at com.ibm.watson.developer_cloud.text_to_speech.v1.TextToSpeech.synthesize(TextToSpeech.java:119)

Is this a bug in the API or am I calling it wrong? I have been using other services in the same API without any problem. It seems to be adding the "Accept" HttpHeader title as a query parameter from what I can tell.

Apologies if it is my error. Rob Wills.

germanattanasio commented 9 years ago

I've fixed the issue, it will be available in the next release. For you I would suggest you to extend the TextToSpeech class and override

InputStream synthesize(final String text, final Voice voice, final String outputFormat)

Example:

public class TextToSpeechHotFix extends TextToSpeech {

  @Override
  public InputStream synthesize(final String text, final Voice voice, final String outputFormat) {
    final RequestBuilder request = RequestBuilder.get(PATH_SYNTHESIZE);
    request.withQuery(TEXT, text);
    request.withQuery(VOICE, voice.getName());

    if (outputFormat != null && !outputFormat.startsWith("audio/"))
      throw new IllegalArgumentException(
          "format needs to be an audio mime type, for example: audio/wav or audio/ogg; codecs=opus");

    request.withQuery(ACCEPT, outputFormat != null ? outputFormat : HttpMediaType.AUDIO_WAV);

    final Response response = execute(request.build());
    return ResponseUtil.getInputStream(response);
  }
}
RobWills commented 9 years ago

German,

Much obliged for such a quick and personal response. Keep up the good work.

Regards,

Rob

I've fixed the issue, it will be available in the next release. For you I would suggest you to extend the TextToSpeech class and override

InputStream synthesize(final String text, final Voice voice, final String outputFormat)

with:

public class TextToSpeechHotFix extends TextToSpeech {

@Override public InputStream synthesize(final String text, final Voice voice, final String outputFormat) { final RequestBuilder request = RequestBuilder.get(PATH_SYNTHESIZE); request.withQuery(TEXT, text); request.withQuery(VOICE, voice.getName()); if (outputFormat != null && !outputFormat.startsWith("audio/")) throw new IllegalArgumentException( "format needs to be an audio mime type, for example: audio/wav or audio/ogg; codecs=opus");

request.withQuery(ACCEPT, outputFormat != null ? outputFormat : HttpMediaType.AUDIO_WAV);

final Response response = execute(request.build());
return ResponseUtil.getInputStream(response);

} }