【问题标题】:Error (HTTP/1.1 500 Internal Server Error) for POST from httpClient but works in Postman来自 httpClient 的 POST 错误(HTTP/1.1 500 内部服务器错误)但在 Postman 中有效
【发布时间】:2019-11-16 18:14:41
【问题描述】:

当我尝试使用 HttpClient 在我的 REST 客户端应用程序中执行 POST 时,我收到了这个错误(HTTP/1.1 500 Internal Server Error)。

请让我知道哪里出了问题。

这是我的代码 sn-p。我包括接受 application/json 作为内容类型的标头

HashMap<String, String> defaultHeaders = new HashMap<String, String>();
defaultHeaders.put("content-type", "application/json");

request = new HttpPost(requestString);
if (sPayload != null) {
    payload = new StringEntity(sPayload);
    ((HttpPost) request).setEntity(payload);
}

【问题讨论】:

  • 我没有看到您在此处粘贴的代码中设置了标题。您可以尝试添加像 request.setHeader("Content-Type", "application/json"); 这样的标头吗
  • 另外,检查你传递的payload是否为json格式。
  • 是的,我添加了标题,现在可以使用了。谢谢!

标签: json rest post apache-httpclient-4.x internal-server-error


【解决方案1】:

请在代码中设置如下标题

request.setHeader("Content-Type", "application/json");

它会起作用的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    • 2013-09-08
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    相关资源
    最近更新 更多