Skip to content

如何从 HttpServletRequest 获取请求内容

🏷️ Java

java
import net.sf.json.JSONObject;

HttpServletRequest request;

StringBuffer sb = new StringBuffer();
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null) {
    sb.append(line);
}
String requestBody = sb.toString();

// 获取 JSON 请求中的某个字段值
JSONObject reagobj = JSONObject.fromObject(jb.toString());
try {
    value = reagobj.getString("token");
} catch (Exception ex) {
    Logger.error(ex.getMessage())
}