【发布时间】:2020-08-28 15:55:48
【问题描述】:
我正在尝试解析一些 cURL 命令:
curl -X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Token token_key" \
-d '{ "query": "query" }' \
https://suggestions.dadata.ru/suggestions/api/4_1/rs/findById/fias
但我的版本:
ProcessBuilder pb = new ProcessBuilder(
"curl",
"-X", "\"POST\"",
"-H", "\"Content-Type: application/json\"",
"-H", "\"Accept: application/json\"",
"-H", "\"Authorization: Token token\"",
"-d", "\"{ \"query\": \"query\" }\"",
"https://suggestions.dadata.ru/suggestions/api/4_1/rs/findById/fias");
抛出错误:
{"family":"CLIENT_ERROR","reason":"Bad Request","message":"Unexpected character ('q' (code 113)):
was expecting double-quote to start field name\n at [Source: (org.apache.cxf.transport.http.AbstractHTTPDestination$1);
line: 1, column: 4]"}2020-08-28 19:37:03.961 ERROR 3692 --- [nio-8088-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] :
Servlet.service() for servlet [dispatcherServlet] in context with path []
threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Failed to decode [/dadata/ % Total % Received % Xferd
Average Speed Time Time Time Current Dload Upload Total Spent Left Speed
有人可以帮忙吗?感谢您的帮助
【问题讨论】:
-
不要引用参数,
ProcessBuilder会根据需要这样做。例如。使用"-X", "POST",而不使用任何\"。当然,您仍然必须引用 JSON 名称和字符串值。 -
@Andreas 谢谢,但这并没有帮助。我现在通过 HttpURLConnection 连接,它可以工作了