0

I am trying to make a request frome a Java app to another app that is written in Spring. Right now I am getting a 400.

This is the Spring endpoint I am trying to reach:

@GetMapping(value="/tts/{sessionid}/{fileId}/{text}")
ResponseEntity<byte[]> getAudioFile(
        @ApiParam(value = "Wave SessionId", required = true) @PathVariable String sessionid,
        @ApiParam(value = "File id", required = true) @PathVariable Integer fileId,
        @RequestParam(value = "Text", required = true) String text
) throws Exception

and here is my attempt to make a valid request:

    private void getTtsWave(String waveId, String token, int file_id, String tts_text) {        
        try {
            URL url = new URL(this.recorderendpoint + "/api/tts/" + waveId + "/" + String.valueOf(file_id) + "/{text}?Text=" + tts_text);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("Authorization", "Bearer " + token);
            int status = con.getResponseCode();
System.out.println(status);
            if (status == 200) {
                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuffer content = new StringBuffer();
                while ((inputLine = in.readLine()) != null) {
                    content.append(inputLine);
                }
                
                if (content != null && !content.equals("")) {
                    System.out.println(content);
                }
                
                in.close();
            }
        } catch (Exception e) {
            log("getTtsWave error: " + e, e.toString()); 
        }
    }
3
  • 4
    Why you have {text} in the end of your url since it is not treated as a path param? Maybe this is the error? Commented Aug 31, 2021 at 9:12
  • as mentioned above text is a required param, so its showing 400 bad request error Commented Aug 31, 2021 at 9:14
  • {text} in new URL() is a string literal but in Controller, it is a path Variable. so you need to change that to text. Commented Aug 31, 2021 at 9:22

1 Answer 1

1
@GetMapping(value="/tts/{sessionid}/{fileId}/{text}")
ResponseEntity<byte[]> getAudioFile

expects that {text} is a @PathVariable but in your getTtsWave method you are treating it as a "static" part of your url:

this.recorderendpoint + "/api/tts/" + waveId + "/" + String.valueOf(file_id) + "/{text}?Text=" + tts_text

Moreover in your getAudioFile you also have a parameter:

@RequestParam(value = "Text", required = true) String text

This one parameter is the required request param (not path param)

So I believe you should change to:

@GetMapping(value="/tts/{sessionid}/{fileId}")
ResponseEntity<byte[]> getAudioFile(
        @ApiParam(value = "Wave SessionId", required = true) @PathVariable String sessionid,
        @ApiParam(value = "File id", required = true) @PathVariable Integer fileId,
        @RequestParam(value = "Text", required = true) String text
) throws Exception

and construct your URL as:

this.recorderendpoint + "/api/tts/" + waveId + "/" + String.valueOf(file_id) + "?Text=" + tts_text
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.