从Curl POST请求中提取数据

时间:2014-06-21 07:24:35

标签: java servlets post curl

我正在使用curl发出POST请求,如下所示

curl -H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-X POST \
-d '{ "name": "PhoneName", "description": "Phone Description!", "details": { "imeiNumber": "123456789123456", "phoneNumber": "9999999999"}}' \
-k \
http://test.domain.com/myTestServer/devices

但是在servlet中我无法获取任何数据。

public void devices(HttpServletRequest request, HttpServletResponse response) throws IOException {

    System.out.println("Got Request for devices");
    System.out.println("Request is : "+ request);
    System.out.println("Name : " + request.getParameter("name"));   //gives null
    System.out.println("description : " + request.getParameter("description")); //gives null
    Enumeration<String> paramNames = request.getParameterNames();   //Empty Enumeration
    while(paramNames.hasMoreElements()){
        String paramName = paramNames.nextElement();
        System.out.println(paramName + " : " + request.getParameter(paramName));

    }

}

有什么建议吗?还有一种更好的方法来获取数据,如"details": { "imeiNumber": "123456789123456", "phoneNumber": "9999999999"}

2 个答案:

答案 0 :(得分:5)

  

-d'{“name”:“PhoneName”,“description”:“Phone Description!”,“details”:{“imeiNumber”:“123456789123456”,“phoneNumber”:“9999999999”}}'\ < / p>

将输入json推送到POST请求有效负载。 而且你不能简单地使用它们的名称解组输入json属性,因此你得到了

System.out.println("Name : " + request.getParameter("name"));   //gives null
此类陈述的

null值。

您需要从HttpServletRequest对象读取POST请求有效负载,然后使用json解析器解析它以获取属性值。

您可以像这样阅读整个POST请求有效负载:

try {
    InputStream inputStream = request.getInputStream();
    if (inputStream != null) {
        bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        char[] charBuffer = new char[128];
        int bytesRead = -1;
        while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
            stringBuilder.append(charBuffer, 0, bytesRead);
        }
    } else {
        stringBuilder.append("");
    }
} catch (IOException ex) {
    throw ex;
} finally {
    if (bufferedReader != null) {
        try {
            bufferedReader.close();
        } catch (IOException ex) {
            throw ex;
        }
    }
}

body = stringBuilder.toString();

现在你应该在变量体中得到你的输入json。现在使用json解析器来获取attribues值,例如:

JsonParser parser = new JsonParser();
JsonObject obj = parser.parse(body).getAsJsonObject();
String name = obj.get("name").toString();

答案 1 :(得分:0)

  

有什么建议吗?也是获取数据的更好方法,如“详细信息”:{“imeiNumber”:“123456789123456”,“phoneNumber”:“9999999999”}

这是一个建议,不要将其视为解决方案

抱歉,我对curl一无所知,但我可以帮助您使用GSON library解析JSON字符串。只需将上面的JSON字符串括在{...}中并将其解析为Java对象,如下面的示例代码所示。 示例代码:

class Details {
    private String name;
    private String description;
    private PhoneNumberDetails details;
    // getter & setter  
}


class PhoneNumberDetails{
    private String imeiNumber;
    private String phoneNumber;
    // getter & setter    
}

String json = "{ \"name\": \"PhoneName\", \"description\": \"Phone Description!\", \"details\": { \"imeiNumber\": \"123456789123456\", \"phoneNumber\": \"9999999999\"}}";
Details data = new Gson().fromJson(json, Details.class);
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(data));

输出:

{
  "name": "PhoneName",
  "description": "Phone Description!",
  "details": {
    "imeiNumber": "123456789123456",
    "phoneNumber": "9999999999"
  }
}