【发布时间】:2015-08-10 11:41:04
【问题描述】:
我正在尝试将JSON 发布到一些REST 服务,但我总是以HTTP Error 415: Unsupported Media Type 结束。
REST 文档 明确指出我应该使用application/json,我这样做了。我肯定忽略了一些东西。
public JSONObject fetchResponse() throws ResourceException, JSONException, IOException {
JRRequest jr = new JRRequest();
jr.setJql(jql);
jr.setMaxResults(Integer.parseInt(maxresults));
jr.setFields(fields);
Gson json = new Gson();
String payload = json.toJson(jr);
JSONObject jsObj = new JSONObject(getClientResource(restUri).post(payload,MediaType.APPLICATION_JSON).getText());
return jsObj;
}
private ClientResource getClientResource(String uri) {
ClientResource clientResource = new ClientResource(uri);
Application app = new Application();
clientResource.setChallengeResponse(ChallengeScheme.HTTP_BASIC,username, password);
return clientResource;
}
【问题讨论】:
-
您有
Restlet服务器吗?还是您在Node或PHP等其他服务器上发帖? -
在不同的服务器上发布,实际上是 JIRA (Tomcat)。
-
我已经在 Chrome 中尝试了高级 REST 客户端,使用 application/json 发布了一个 json 有效负载,它工作正常。所以服务器肯定工作正常。
-
您是否检查过
JSON是否格式错误或是否在请求标头上记录了一些日志?打印标题显示在此链接中stackoverflow.com/questions/17743785/…