POST 对 RESTful API 的请求总是导致 HTTP 400 语法不正确的错误消息
POST request to RESTful API always results in HTTP 400 Syntactically incorrect error message
我在我的 Web 服务器中使用 Jersey 实现了 RESTful API 来处理简单的 POST
请求。
方法如下:-
@Path("create")
public class Create {
.........
@POST
@Path("/{id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public StatusResponse createPost(
@PathParam("id") final String identifier,
final PostInfo postInfo) {
// Does its work
}
下面是 PostInfo.java : -
public class PostInfo {
private String message;
private String date;
public PostInfo() {
message = null;
date = null;
}
/**
* @return the message
*/
public String getMessage() {
return message;
}
/**
* @return the date
*/
public String getDate() {
return date;
}
/**
* @param message the message to set
*/
public void setMessage(final String message) {
this.message = message;
}
/**
* @param date the date to set
*/
public void setDate(final String date) {
this.date = date;
}
}
这里的问题是,每次我使用 JSON 元素发出 POST 请求时,都会导致
HTTP Status 400 - Bad Request The request sent by the client was syntactically incorrect.
我看了其他几个有类似问题的帖子 (this and this),但他们没有帮助。
我正在使用 Chrome 的 POSTMan 在以下 URL 处发出请求,正文如下:
URL: wallpostapi.herokuapp.com/webapi/create/xyz123@123.com
正文:
{
"message" : "WHADDUP DAWG???"
"date" : "2016-09-09 11:00:20"
}
顺便说一句:HTTP 规范描述了一个 POST 来创建一个列表的子条目。所以 POST 应该转到 uri wallpostapi.herokuapp.com/webapi/create
如果你知道 id 你应该使用 HTTP 的 PUT 方法
您的 JSON 语法不正确,缺少一个逗号。请尝试以下 JSON.
{
"message": "WHADDUP DAWG???",
"date": "2016-09-09 11:00:20"
}
此外,最佳做法是在 URI 中使用名词而不使用动词,例如,
http://wallpostapi.herokuapp.com/webapi/v1/info
ID 可以作为可选参数在 header 中发送,也可以为创建自动生成。
id xyz123@123.com
创建方法的名称应该是,
public StatusResponse createInfo(
@HeaderParam("id") final String identifier,
final PostInfo postInfo {
// Does its work
}
我在我的 Web 服务器中使用 Jersey 实现了 RESTful API 来处理简单的 POST
请求。
方法如下:-
@Path("create")
public class Create {
.........
@POST
@Path("/{id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public StatusResponse createPost(
@PathParam("id") final String identifier,
final PostInfo postInfo) {
// Does its work
}
下面是 PostInfo.java : -
public class PostInfo {
private String message;
private String date;
public PostInfo() {
message = null;
date = null;
}
/**
* @return the message
*/
public String getMessage() {
return message;
}
/**
* @return the date
*/
public String getDate() {
return date;
}
/**
* @param message the message to set
*/
public void setMessage(final String message) {
this.message = message;
}
/**
* @param date the date to set
*/
public void setDate(final String date) {
this.date = date;
}
}
这里的问题是,每次我使用 JSON 元素发出 POST 请求时,都会导致
HTTP Status 400 - Bad Request The request sent by the client was syntactically incorrect.
我看了其他几个有类似问题的帖子 (this and this),但他们没有帮助。
我正在使用 Chrome 的 POSTMan 在以下 URL 处发出请求,正文如下:
URL: wallpostapi.herokuapp.com/webapi/create/xyz123@123.com
正文:
{
"message" : "WHADDUP DAWG???"
"date" : "2016-09-09 11:00:20"
}
顺便说一句:HTTP 规范描述了一个 POST 来创建一个列表的子条目。所以 POST 应该转到 uri wallpostapi.herokuapp.com/webapi/create
如果你知道 id 你应该使用 HTTP 的 PUT 方法
您的 JSON 语法不正确,缺少一个逗号。请尝试以下 JSON.
{
"message": "WHADDUP DAWG???",
"date": "2016-09-09 11:00:20"
}
此外,最佳做法是在 URI 中使用名词而不使用动词,例如,
http://wallpostapi.herokuapp.com/webapi/v1/info
ID 可以作为可选参数在 header 中发送,也可以为创建自动生成。
id xyz123@123.com
创建方法的名称应该是,
public StatusResponse createInfo(
@HeaderParam("id") final String identifier,
final PostInfo postInfo {
// Does its work
}