restful architecture - REST API: how to notify a client that the request has failed when the service already returned 200 and some data? -


rest api: how notify client request has failed when service returned 200 , data?

what doing? developing rest web service returns data 2 sources:

  1. an csv file http server changes , huge.
  2. a local file.

when client invokes service, this:

  1. it sends request http server obtain csv file.
  2. after obtaining csv file, combines data both sources.
  3. sends result client. result xml document.

problem sometimes, after have returned data client, http server fails cannot continue sending data client. when happens, notify client there error. how should this? service returned http code 200 , data. cannot send client error 500.

should write output error message? client fail because xml-document not valid.

the service cannot wait send response until entire file http server read. reasons file obtained http big , not fit in memory.

environment: although not think important, service developed in jersey 1.x.

as say, there couple options:

  • start sending response 200 ok before upload request complete, rely on client detect invalid ontent response; or
  • wait until request file upload complete before sending http response. can send correct status code (2xx or 500).

i recommend waiting until upload complete.

if file cannot fit in server-side memory, find technique write stream persistence not in memory, such cache, nosql db, or filesystem. allow faster processing of file upload.

if require additional time process file on server side after upload, can return 202 accepted status, location: header having resource long-running job. client can keep checking if job complete. avoid having process whole thing in 1 http round-trip.

some examples of using restful long-operations: