0
Not a bug

Java: ошибка 400 при обращении к подсказкам

d.ivanov 3 years ago updated 1 year ago 3

При передаче параметр в сервис подсказки (организации), получаю ошибку: java.io.IOException: Server returned HTTP response code: 400 for URL: https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/party


Код:

private String fetchJsonSuggest(String method, String... sources) throws Exception{
    String toReturn = null;
    method = "suggest/party";
try {
URL url = new URL("https://suggestions.dadata.ru/suggestions/api/4_1/rs/" + method);
URLConnection connection = url.openConnection();
connection.addRequestProperty("Content-Type", "application/json");
connection.addRequestProperty("Accept", "application/json");
connection.addRequestProperty("Authorization", "Token " + authKey);

if (sources.length > 0) {
connection.setDoOutput(true);
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.write(gson.toJson(sources).getBytes());
outputStream.flush();
outputStream.close();
}

InputStream inputStream = connection.getInputStream();
toReturn = IOUtils.<em>toString</em>(inputStream, "UTF-8");
inputStream.close();
} catch (IOException e) {
        LOGGER.error("Error fetchJsonSuggest data", e);
}

    return toReturn;
}

Решено самостоятельно.

и как же решено?
у меня сейчас такая же ошибка,  не могу понять.