-1

We are using HTTP protocol to trigger an asynchronous action on the web server, which later in the background loads data from another server and writes them into our server's database.

 ┌──────┐              ┌──────┐  ┌───────────┐
 │client│              │server│  │data-source│
 └──┬───┘              └──┬───┘  └─────┬─────┘
    │                     │            │      
    │ ?METHOD? /sync-data │            │      
    │────────────────────→│            │      
    │                     │            │      
    │    202 ACCEPTED     │            │      
    │←────────────────────│            │      
    │                     │            │      
    │                     │ Fetch data │      
    │                     │───────────→│      
    │                     │            │      
    │                     │   [data]   │      
    │                     │←───────────│      
    │                     │            │      
    │                     │───────╮    │      
    │                     │ Store │    │      
    │                     │ data  │    │      
    │                     │←──────╯    │      
    │                     │            │      
 ┌──┴───┐              ┌──┴───┐  ┌─────┴─────┐
 │client│              │server│  │data-source│
 └──────┘              └──────┘  └───────────┘

(Credit for ASCII sequence diagram online tool: https://arthursonzogni.com/Diagon/#Sequence)

The question is, which HTTP method is the best for the call?

I am not asking for an opinion, but for well-informed answer which of the HTTP method is more corresponding to the industry standards in this particular case.

I collected arguments for and against each of the method.

In "Normal purpose" I am influenced how we use CRUD-HTTP mapping with REST.

Method Normal purpose Why to use it in my case Why not
GET Read data • Request does not immediately change server status
• No body
• Most simple for testing
Eventually server status changed
POST Create a new object • It is a common method used for changes • The character of the triggered asynchronous action is not Create
• After the method returns, the server status is not yet changed
PUT Replace an existing object • Sounds fine, but... • The data which will replace the server data are not part of this request
• After the method returns, the server status is not yet changed
DELETE Delete an existing object N/A N/A
PATCH Partially update an existing object N/A • We are not changed "some fields of a record"
• The data which will replace the server data are not part of this request
• After the method returns, the server status is not yet changed

EDIT After receiving downvoting and close suggestions as "Opinion-based"

I am not asking anyone's opinion, what I want is to know which of the HTTP method corresponds the best with the above mentioned use case, according to standards. I might have some opinion on it, but I want my opinion to be supported by what the industry standard is. My arguments and counter-arguments collected in the table might look like "opinion-based", but that's how people are searching for an answer - sorting out what they know so far.

Please consider this before quickly connecting the phrasing "which is the best" with "opinion-based".

3
  • 1
    "POST - Create a new object" - That's a very narrow interpretation... Commented Mar 1, 2024 at 7:56
  • POST - because you will be you are sending data to perform actions, not to get information. Also it's default method to send information to server. PUT/PATH/DELETE is more for REST API and is "made up" (HTML forms does not have these methods) Commented Mar 1, 2024 at 8:00
  • @Justinas They aren't "made up" - they're part of the HTTP specifications going back decades and (excepting HTTP/0.9), HTTP has always had methods that aren't directly usable from HTML. Commented Mar 1, 2024 at 8:01

1 Answer 1

1

The question is, which HTTP method is the best for the call?

POST


Your notion that the POST method's "normal purpose is for creating a new object" is misguided.

In actuality, the current HTTP specification (RFC 9110, June 2022) describes POST thusly (emphasis mine):

The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics.

For asynchronous processing of requests, HTTP server should respond with HTTP 202 Accepted:

The 202 (Accepted) status code indicates that the request has been accepted for processing, but the processing has not been completed.
[...]
The 202 response is intentionally noncommittal. Its purpose is to allow a server to accept a request for some other process (perhaps a batch-oriented process that is only run once per day) without requiring that the user agent's connection to the server persist until the process is completed. The representation sent with this response ought to describe the request's current status and point to (or embed) a status monitor that can provide the user with an estimate of when the request will be fulfilled.

Sign up to request clarification or add additional context in comments.

4 Comments

I was probably too much influenced by the REST standards, where CRUD - HTTP mapping usually maps POST to CREATE and PUT to UPDATE.
@HonzaZidek There is no REST standard.
"de facto" standard then? :) en.wikipedia.org/wiki/De_facto_standard
@HonzaZidek Well, no, because there isn't even a specification for REST. What we actually have is a vaguely-defined notion that REST means "mapping HTTP resources to one's domain model entities, and also mapping HTTP methods/verbs to CRUD/RPC actions" - it's far better than SOAP, sure - but really isn't what REST is/was really about. Required reading: steveklabnik.com/writing/nobody-understands-rest-or-http

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.