解析Java代码中的cURL命令导致错误。

huangapple go评论99阅读模式
英文:

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/

huangapple
  • 本文由 发表于 2020年8月28日 23:55:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63637188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定