2

I'm writing a REST web service that translates texts from one language to another. The text can be fairly large, up to a few megabytes.

What HTTP verb is best to use?

My first thought was to use GET because it gets you the translation for the given text. But the 4K URL length restriction will not do for large texts. The use of the message body with GET is discouraged.

My second thought was to use POST so I can pass the text in the request body. But it doesn't seem to agree with the spirit of HTTP. POST is used to create things; it updates the state of the server while in my case no state is being updated.

What verb would you use?

N.B. Google Translate uses POST.

Sergey Slepov
  • 1,861
  • 13
  • 33
  • 3
    REST is about "resources". Computational operations like "translate this text into Japanese" or "add 1 to 5", etc, do not generally fit well into REST. For operations like this, POST is a general purpose verb in my view. – realharry Nov 26 '17 at 00:42
  • 1
    Read the definition for POST in the RFC, found at https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html - Nothing in that definition is violated by you supplying text in the HTTP request body and receiving a translation in the response body. The "resource" name should definitely be a noun like `translations` and NOT a verb like `translate` though. – Ray Toal Nov 26 '17 at 00:47

1 Answers1

3

I'm learning towards POST, as i see PUT, PATCH, DELETE etc. as specific options.

GET would be the 'appropriate', as its origin is "GET a given resource", eg. fetch my a translation, based on some data you parse along.

However I believe it can be argued, that the translation does not exist, but is merely generated, therefore you POST your string, and the webservice creates the translations, and returns it. Also, as you mention, GET requests are blocked by the URL Length.

Frederik Spang
  • 3,379
  • 1
  • 25
  • 43
  • 1
    You'd *POST* something to the server to be translated. If the result cannot be computed immediately then some token would be issued that could be retrieved later with *GET*. – tadman Nov 26 '17 at 00:46
  • 2
    Exactly my point. `POST` something to be translated, and yeah, if it's not immediately available - You could just return a Translation-resource, that would have state of Pending, or something like that. Good point, about the later fetching - That would be a GET-request, no question. – Frederik Spang Nov 26 '17 at 00:47