Angular 2 Http Post将Content-Length标头设置为0

时间:2017-03-30 17:17:47

标签: angular post

我需要进行一次内容长度等于0的POST调用。所以我在调用时尝试将标头设置为0,但在接收调用的Web服务中,内容长度远大于0

请求

let url = '/blah/blah/'+ anUID +'/blah/' + anotherUID + '';
let headers = new Headers({'Content-Length': '0'});
let options = new RequestOptions({
  method: RequestMethod.Post,
  headers: headers,
});

return this.http.post(url, options)
  .map(this.extractData)
  .catch(this.handleError.bind(this));

接收网络服务中的on有一项检查,以确保请求的内容长度为0.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("{anUID}/blah/{anotherUID}")
public Response myWebService(@PathParam("anUID") Integer anUID, @PathParam("anotherUID") Integer anotherUID,
        @Context ServletContext application, @Context HttpServletRequest request, @Context UriInfo info) {
    Response resp = Response.notModified().build();
    String method = request.getMethod();    // == "POST"
    int meh = request.getContentLength();   // = 172 NEEDS TO BE 0

    if (request.getContentLength() < 1) {
       ...
    }

    ...
}

如果将内容长度设置为0,我可以做些什么?

1 个答案:

答案 0 :(得分:2)

您无需设置标题。只需发送空体即可。

return this.http.post(url, '')
  .map(this.extractData)
  .catch(this.handleError.bind(this));