RestTemplateBuilder:向音频电报API发送音频时出现问题

时间:2019-06-01 10:05:39

标签: java telegram-bot resttemplate sendfile

我使用RestTemaplteBuilder将POST请求发送到Telegram Bot API。 创建上传音频文件的请求时出现问题。 根据文档-https://core.telegram.org/bots/api#sending-files,您需要发送multipart / form-data类型的请求。 错误:413请求实体太大

        MultiValueMap<String, Object> request= new LinkedMultiValueMap<String, Object>();
    try {
        parts.set("chat_id", "id");
        parts.set("audio", (Files.readAllBytes(Paths.get(ClassLoader.getSystemResource("name.mp3").toURI()))));
    } catch (Exception e) {
        e.printStackTrace();
    }
        HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(request, headers);

    ResponseEntity<String> responseEntity = restTemplateBuilder.build().postForEntity(requestFormatter(URL_BOT_PREFIX, method), requestEntity, String.class);

0 个答案:

没有答案