英文:
Parsing cURL command from Java code causing an error
问题
我正在尝试解析一些 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
有人可以帮忙吗?谢谢任何帮助
英文:
i'm trying to parse some cURL command:
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
but my version:
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");
throwing an error:
{"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
Could someone help plz? Thanks for any help
答案1
得分: 0
也许对某人有帮助
String request = "https://suggestions.dadata.ru/suggestions/api/4_1/rs/findById/fias";
URL url = new URL(request);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "Token token");
JSONObject query = new JSONObject();
query.put("query", "query");
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(query.toString());
wr.flush();
InputStream inputStream = conn.getInputStream();
在这里找到了解决方案:https://www.it-swarm.dev/es/java/post-solicitud-enviar-datos-json-java-httpurlconnection/1043548240/
英文:
Maybe it will be helpfull for someone
String request = "https://suggestions.dadata.ru/suggestions/api/4_1/rs/findById/fias"
URL url = new URL(request);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "Token token");
JSONObject query = new JSONObject();
query.put("query", "query");
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(query.toString());
wr.flush();
InputStream inputStream = conn.getInputStream();
Found the solution here https://www.it-swarm.dev/es/java/post-solicitud-enviar-datos-json-java-httpurlconnection/1043548240/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论