This is my curl command
curl -X PATCH -H "accept: application/json" -H "content-type: application/json" -d "{ \"name\": \"guest\" }" --url http://localhost:6080/REST/users/gust.
How do I extract the json data { name: guest}
from the request. Does the HttpServletRequest class has any supporting method?.
Thank you.
use Reader:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
StringBuilder sb = new StringBuilder();
BufferedReader reader = request.getReader();
try {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} finally {
reader.close();
}
System.out.println(sb.toString());
}
or you can try parse the json from the request object using a library, an example is org.json which can do this for you quite easily.... here is some sample code to get you started:
import org.json.*;
JSONObject obj = new JSONObject(" .... ");
String pageName = obj.getJSONObject("pageInfo").getString("pageName");
JSONArray arr = obj.getJSONArray("posts");
for (int i = 0; i < arr.length(); i++)
{
String post_id = arr.getJSONObject(i).getString("post_id");
......
}
Downloadable jar: http://mvnrepository.com/artifact/org.json/json
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.