1

I'm developing an API that given a few parameters, reads the given data, transforms it and returns it as JSON.

Eg. (some logic included)

Request:
POST /myApi/exportAsText
Content-Type: application/json
{
    section: array,
    answers: array,
    targetLevel: int
}

Output:

// Goes through data, create big text
{'text': "blablablabla"}

Currently, I'm using POST requests to send the data, however I think it is against best-practices to use POST requests unless we are creating resources.

Now, you ask me: Why not just use GET requests?

Answer: Url length issues. Some parameters that I'm sending are 3000 chars long, which is way over the max url length in some browsers (eg. Edge 16 (2047 max length)).

I've had a look at this post, which is about a very similar topic. They recommend creating a file system that would work the following way:

  1. Send POST request, create a file with text output named using uuid4() and return the file name.

  2. Send GET Request with the file name, return the file content and delete the file.

  3. If the file, is not acessed in a few minutes, delete it automaticaly.

I would like to know, what you guys think about this approach and if there is other good approach.

Thank you and I apologize if there is any grammar mistakes.

1
  • Just a note. REST is meant to an end, not the end itself. Some people strive to put REST in the middle of the solution. Solve the problem in the easiest way possible. If you don't need a cache, then this solution you have read is overengineering. Even if you don't need it. It's not even contemplating the possibility of POST returning a 307 with the URI where the content will be served eventually. URI that must be consumed with POST too.
    – Laiv
    Commented Jun 7, 2022 at 13:49

1 Answer 1

2

Using POST requests is perfectly fine. The POST request method is in a sense a catch-all for when no other method is appropriate. In particular, using POST is appropriate for doing some processing on your server, regardless of whether this causes resources to be created.

Your POST–redirect–GET approach sounds overly complicated for your use case. And complex code breeds bugs. So I wouldn't got that route unless you had a particular reason. One reason might be that you want to support caching of the output. By itself, a POST request is not cacheable or repeatable. In contrast, creating a resource that can be accessed via GET could allow multiple clients to access the same result, or to allow a client to access the result later without having to store it locally. But those are special requirements that you probably don't have.

2
  • How important is caching? Do you think its worth doing the POST–redirect–GET approach jsut to have caching? Commented Jun 5, 2022 at 12:38
  • 1
    @RubenAntonioNunesLousada It all depends on what actually will be done for each POST request. It would be unusual to rely on HTTP caching like this. If caching is necessary for performance, the server could do its own caching. Outside of REST but for normal web forms, POST-and-redirect is very commonly used though to solve the double-submit problem.
    – amon
    Commented Jun 5, 2022 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.