Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Construct a POST Request Which Expects No Body

I've an HTTP client sending many POST requests to a server. The server responds to all requests with 201 Created and a response body. For my purposes, the response header is enough, as I'm only interested in the Location header. I'd like to avoid that the server produces a response body in order to significantly decrease network traffic.

According to RFC 7231, ...

  [...] if one or more resources has been created on the origin server as a
  result of successfully processing a POST request, the origin server
  SHOULD send a 201 (Created) response containing a Location header [...]

..., thus, I assume, the server COULD also respond e.g. with 204 No Content, omiting the body.

Therefore my question: Is it possible to construct a POST request which makes the server respond with 204 No Content or to omit the response body in another way?

Update 1: The server side is a Spring Data REST project and I'm free to configure it. I know that I could set RepositoryRestConfiguration#setReturnBodyOnCreate to false, but that would be overdone as it affects all incoming requests. Therefore, I'd prefer to make the decision on the client side.

like image 335
aboger Avatar asked Nov 20 '18 10:11

aboger


2 Answers

There's no real lever you can pull from the client side to control if the server will respond with a body or not, unless the service you work with has a specific feature that allows this.

A header that a server might use is Prefer: return=minimal but if the service doesn't explicitly document support for this, chances are low that this will work.

Really the only think you can do one the client is to:

  1. Kill the TCP connection as soon as you got the response headers
  2. Kill the HTTP/2 stream when you recieved the headers.

This is a pretty 'drastic' thing but clients do use this mechanism for some cases and it does work. However, if the POST response body was somewhat small there's a chance that it's not really making a ton of difference because the response might already have been sent.

like image 124
Evert Avatar answered Nov 03 '22 17:11

Evert


There is no way to do it client side only as it is not natively implemented in Spring REST server.

Anyway, any client demand can be transformed as an extra custom header or a query parameter in the request.

A way could be to override default response handlers and detect custom header (implements Prefer: return=minimal as suggested before for instance) and/or query param presence to trigger an empty response with a 204 status. This post may help you to figure it out.

like image 41
Bertrand Avatar answered Nov 03 '22 15:11

Bertrand