【发布时间】:2020-04-19 14:11:30
【问题描述】:
我正在使用 Java 中的 curl 命令字符串发送 POST 请求。我在值中有空格的命令字符串中传递 json。当编译器在 json 中遇到空格时出现错误。我需要保留空格并传递字符串 curl 命令中的值。请帮忙。如果有人可以重写我的 string[] 命令来帮助我理解我的错误,我会很棒。这是我的代码。
String[] command = { "curl", "-X", "POST", "http://my.url.com/add", "-H", "accept: application/json", "-H", "AuthorizationToken: 123", "-H", "Content-Type: application/json", "-d", "{\"FieldLabels\":\"Name,Status,Employee number\",\"FieldValues\":\"test7,Planned,Raj Kumar(123)\",\"Type\":\"BT\"}" };
ProcessBuilder process = new ProcessBuilder(command);
Process p;
try
{
p = process.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
String result = builder.toString();
System.out.print(result);
}
catch (Exception e)
{
e.printStackTrace();
}
Error:
{
"message" : {
"statusCode" : "500",
"Status" : "Internal Server Error",
"requestedURI" : "/api/EFormService/createEFormItemData",
"error" : "Expected a ':' after a key at character 25 of {FieldLabels:Name,Status,Employee"
}
}
【问题讨论】:
标签: java json string curl space