Java read data POST content
I have an API that i'm testing. The API receive POST Request and read it like this
StringBuffer jb = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null)
jb.append(line);
System.out.println("jb: "+jb);
System.out.println("request.getHeader('content-type'): "+request.getHeader("content-type"));
} catch (Exception e) { /*report an error*/ }
All works fine when I send a POST request in "application/json;charset=utf-8"
httpPost.setHeader("content-type", "application/json;charset=utf-8");
It prints this:
jb: {"client_domain":"=....); //proper Json data
request.getHeader('content-type'): application/json;charset=utf-8
And I can read the data properly.
However my problem is when I send the data the same way but I set the content-type "application/x-www-form-urlencoded;charset=utf-8"
httpPost.setHeader("content-type", "application/x-www-form-urlencoded;charset=utf-8");
The test is the same just the content-type is different but then it seems that I don't receive any data anymore:
jb:
request.getHeader('content-type'): application/x-www-form-urlencoded;charset=utf-8
Any idea?
/// Update
Here is the Spring Controller
@RequestMapping(value = {"user/add"}, method = RequestMethod.POST, produces="application/json; charset=utf-8")
@ResponseBody
public ResponseEntity<String> getNewUserApi(HttpServletRequest request,
HttpServletResponse response) throws Exception {
Map<String, Object> jsonObj = new HashMap<String, Object>();
StringBuffer jb = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null)
jb.append(line);
System.out.println("jb: "+jb);
System.out.println("request.getHeader('content-type'): "+request.getHeader("content-type"));
} catch (Exception e) { /*report an error*/ }
///I create my JSon that will be sent back
return JsonUtils.createJson(jsonObj);
//UPDATE 2 Here is how I send the Data
public static void main(String[] args) throws Exception {
String url = "http://localhost:8080/child/apiv1/user/add";
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("content-type", "application/x-www-form-urlencoded;charset=utf-8");
try {
//we had to the parameters to the post request
JSONObject json = new JSONObject();
json.put("client_id", "fashfksajfhjsakfaskljhflakj");
json.put("client_secret", "9435798243750923470925709348509275092");
json.put("client_domain", "dummy.localhost.com");
//create the user json object
JSONObject userObj = new JSONObject();
userObj.put("email", "johnsmith42@yopmail.com");
userObj.put("name", "Anna Sax");
JSONArray childrenArray = new JSONArray();
JSONObject child1 = new JSONObject();
child1.put("name", "Iphone 6");
child1.put("age", "2");
childrenArray.put(child1);
userObj.put("children", childrenArray);
json.put("user", childObj);
StringEntity params = new StringEntity(json.toString());
httpPost.setEntity(params);
System.out.println("executing request: " + httpPost.getRequestLine());
HttpResponse response;
response = client.execute(httpPost);
//[...]
} //End main
I know it doesn't really make sense to create a Json and send it in "application/x-www-form-urlencoded" but it's just that one of our user can't fix his issue and it will only send "application/x-www-form-urlencoded".
@RequestMapping的'produce'属性表明客户端只能接受application / json数据,因此您可以将其删除或将其更改为'application / x-www-form-urlencoded'。
链接地址: http://www.djcxy.com/p/22228.html上一篇: SAML与通过OAuth进行联合登录
下一篇: Java读取数据POST内容