我正在使用Java中的curl命令字符串发送POST请求。我在命令字符串中传递json,该字符串的值中有空格。当编译器在json中遇到空间时出现错误。我需要保留空格并在string 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"
}
}
尝试将json请求放置在文件中,并在curl请求中使用以下格式
--data "@<path/to/file>"
'''表示请求数据在以下路径中的文件中。