我有一个JavaScript代码,我在其中发送带有一些参数的http帖子。 Post参数是一个看起来像这样的json:
{"tokenRequest":{"authSpecification":{"authToken":"T7SUNv0j2eRTeu04tVbcSa0LHN1YnNjcmliZXItMTk2LDEsRk9YVEVMLDE5NiwxMT"},"contentSpecification":{"contentId":"abc"}}}
在JavaScript中,我只是打开请求,设置标题和发送参数。发布请求看起来像:
var request = new XMLHttpRequest();
request.open("POST", url, true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
request.send(tokenRequestJSON); //tokenRequestJSON is the json parameter mentioned above
现在我需要用Java进行相同的调用(由于某些内部POC要求)。为此,我做了以下事情:
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
Map<String,String> httpHeaders = new HashMap<>();
httpHeaders.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
for (Map.Entry header : httpHeaders.entrySet()) {
asyncHttpClient.addHeader((String)header.getKey(), (String)header.getValue());
}
RequestParams postData1 = new RequestParams();
String tokenRequest1 = "{\"tokenRequest\":{\"authSpecification\":{\"authToken\":\"T7SUNv0j2eRTeu04tVbcSa0LHN1YnNjcmliZXItMTk2LDEsRk9YVEVMLDE5NiwxMT\"},\"contentSpecification\":{\"contentId\":\"abc\"}}}";
postData1.put("arg0", tokenRequest1);
asyncHttpClient.post(url, postData1, new ResponseHandler());
但这给了我错误。 {"errorResponse": {"status": "ERROR", "errorCode": "MDRM-0002", "errorMessage": "Json body not properly formed (No JSON object could be decoded)"}}
我是Java的新手,我可能缺少一些基本知识。您知道吗,为什么来自Java的请求失败了?
谢谢。
答案 0 :(得分:1)
您在使用AsyncHttpClient吗?
RequestParams postData1 = new RequestParams();
postData1.put("arg0", tokenRequest1) // this is not a json object, not the body of the request.
请求参数是URL上的参数,例如,“日期”是请求参数: http://localhost:8080/MyApp/user/1234/invoices?date=12-05-2013