0

Необходимо сделать запрос, но не совсем понимаю как, а именно как задать данные необходимые для передачи. На cURL запрос выглядит так:

curl --request POST \
  'https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  --data '{"snippet":{"channelId":"UCLGnG6SffG60QKqw-PuCjng","videoId":"dTUPnrAZESU","topLevelComment":{"snippet":{"textOriginal":"Test comment"}}}}' \
  --compressed

На Java "реализую" это так:

String url = "https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&key="+apikey;
        String urlParameters = "data={\"snippet\":{\"channelId\":\"UCLGnG6SffG60QKqw-PuCjng\",\"videoId\":\"dTUPnrAZESU\",\"topLevelComment\":{\"snippet\":{\"textOriginal\":\"Test comment\"}}}}";
        byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);

        try {

            URL myurl = new URL(url);
            con = (HttpURLConnection) myurl.openConnection();

            con.setDoOutput(true);
            con.setRequestMethod("POST");
            con.setRequestProperty("Authorization", "Bearer "+key);
            con.setRequestProperty("Accept", "application/json'");
            con.setRequestProperty("Content-Type", "application/json'");

            try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {

                wr.write(postData);
            }

            StringBuilder content;

            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(con.getInputStream()))) {

                String line;
                content = new StringBuilder();

                while ((line = br.readLine()) != null) {
                    content.append(line);
                    content.append(System.lineSeparator());
                }
            }

            System.out.println(content.toString());

        } finally {

            con.disconnect();
        }

Я не понимаю куда прописать json для отправки в cURL это --data

2
  • 1
    Так Вы уже положили их в postData. Только уберите data= в строке
    – Tsyklop
    25 июл 2020 в 10:29
  • @Tsyklop благодарю! 25 июл 2020 в 10:37

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.