dri*_*imk 7 java post content-type spring-mvc mime-types
我有一个正在测试的 API。API 接收 POST 请求并像这样读取它
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*/ }
Run Code Online (Sandbox Code Playgroud)
当我在“application/json;charset=utf-8”中发送 POST 请求时,一切正常
httpPost.setHeader("content-type", "application/json;charset=utf-8");
Run Code Online (Sandbox Code Playgroud)
它打印这个:
jb: {"client_domain":"=....); //proper Json data
request.getHeader('content-type'): application/json;charset=utf-8
Run Code Online (Sandbox Code Playgroud)
我可以正确读取数据。
但是我的问题是当我以相同的方式发送数据但我设置了内容类型“application/x-www-form-urlencoded;charset=utf-8”
httpPost.setHeader("content-type", "application/x-www-form-urlencoded;charset=utf-8");
Run Code Online (Sandbox Code Playgroud)
测试相同,只是内容类型不同,但似乎我不再收到任何数据:
jb:
request.getHeader('content-type'): application/x-www-form-urlencoded;charset=utf-8
Run Code Online (Sandbox Code Playgroud)
任何的想法?
/// 更新
这是弹簧控制器
@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);
Run Code Online (Sandbox Code Playgroud)
//更新2这是我发送数据的方式
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
Run Code Online (Sandbox Code Playgroud)
我知道创建 Json 并在“application/x-www-form-urlencoded”中发送它真的没有意义,但只是我们的一个用户无法解决他的问题,它只会发送“application/x -www-form-urlencoded”。
小智 -2
@RequestMapping 的 'products' 属性表明客户端只能接受 application/json 数据,因此您可以将其删除或将其更改为 'application/x-www-form-urlencoded'。
| 归档时间: |
|
| 查看次数: |
18615 次 |
| 最近记录: |