Text Analysis

БЕЗКОШТОВНО
(Ким) Bhavya Gaur | Оновлено 9 mesi fa | Text Analysis
Популярність

9.9 / 10

Затримки

514ms

Рівень обслуговування

100%

Повернутися до всіх обговорень

"validation error, incorrect data passed to the server"

avatar
karimo94
5 mesi fa

I am making a post request to the text summarizer endpoint using HttpUrlConnection.

				URL url = new URL(apiUrl);
				HttpURLConnection client = (HttpURLConnection) url.openConnection();
				client.setRequestProperty("content-type", "application/json");
				client.setRequestProperty("x-rapidapi-host", "text-analysis12.p.rapidapi.com");
				client.setRequestProperty("x-rapidapi-key", "hidden");
				client.setRequestMethod("POST");
				client.setDoOutput(true);

				String jsonBody = "{" +
					"\"language\":\"english\"," +
					"\"summary_percent\":" + summaryPercent + "," +
					"\"text\":" + "\"" + myTestString + "\"" + "}";

				//POST to endpoint first
				try(OutputStream os = client.getOutputStream()) {
					byte[] input = jsonBody.getBytes("utf-8");
					os.write(input, 0, input.length);
				}

For some reason, when I make the request via RapidApi it works, but when I do it via Java the response is always this:

{3 items
"time_taken":0.0007703304290771484
"msg":“validation error, incorrect data passed to the server”
“ok”:false
}

I have made sure my JSON body passed in is in good format. Any ideas?

avatar
gaurmanojkumar530 commented 4 mesi fa

We are glad that you were able to solve the issue. Thank you for choosing us.

avatar
karimo94 commented 5 mesi fa

I have solved it. The way to create the JSON payload is to use Gson library by putting all the request parameters into a map, then using Gson to convert it into proper json. The request now works, thank you.

avatar
karimo94 commented 5 mesi fa

@gaurmanojkumar530 https://devqa.io/how-to-convert-java-map-to-json/

what parameters are we converting from the map to JSON? is it the headers? or the JSON payload itself?

avatar
gaurmanojkumar530 commented 5 mesi fa

I am not familiar with Java.
But do you have a way to convert a Dictionary or Key-Value pair to JSON in Java.
This is the way we do it in python.
It is best to use a dedicated library for this purpose.

Приєднуйтесь до обговорення — додайте повідомлення нижче:

Вхід / Реєстрація, щоб публікувати нові повідомлення
Рейтинг: 4 - Голосів: 4