Oleg Sh
Oleg Sh

Reputation: 9001

Web API - correct response to client

We have the following format of response to client if request is successful:

{
  "ExitCode": 1,
  "ErrorMessage": "",
  "NumberOfGreenBoxMatches": 4,
  "NumberOfPinkBoxExtractFrames": 5,
  "ProcessingTime": 4000,
  “HasOverlay”: false;
  "MatchResult": "...."
}

If a client request is bad or internal error is happened customer proposes to return the following:

{
  "ExitCode": -10,
  "ErrorMessage": "Internal error parsing pink boxes. Please contact tech support",
  "NumberOfGreenBoxMatches": 0,
  "NumberOfPinkBoxExtractFrames": 0,
  "ProcessingTime": 1240,
  “HasOverlay”: false;
  "MatchResult": ""
}

is it correct approach (return the similar json)? My opinion is no, we should return only like

{ "ErrorMessage": "Internal error parsing pink boxes. Please contact tech support" }

and correct HttpCode. What is correct way?

Upvotes: 0

Views: 57

Answers (2)

Pranesh Janarthanan
Pranesh Janarthanan

Reputation: 1194

The result from Web API is received using a Model, Here the Model used in Client side is

public class MyResult
{
    public int ExitCode { get; set; }
    public string ErrorMessage { get; set; }
    public int NumberOfGreenBoxMatches { get; set; }
    public int NumberOfPinkBoxExtractFrames { get; set; }
    public int ProcessingTime { get; set; }
    public bool HasOverlay { get; set; }
    public string MatchResult { get; set; }
}

if you send a different result upon error, which has single property "ErrorMessage", They cannot process it on client side.

so you better provide the information as per the exact model proposed by your customer.

Upvotes: -1

Alexander Yancharuk
Alexander Yancharuk

Reputation: 14571

Your suggestion about correct approach looks more HTTP-standart compatible. Because ExitCode looks like HTTP-response codes functionality duplication.

But final solution i think is highly depends on client architecture. There is some questions that you must take into account:

  • Does client parse HTTP codes?
  • How client validates server responses?
  • Does server error-responses MUST be JSON-schema compatible with regular responses?
  • Does your client must always receive values in fields like ProcessingTime etc.

Upvotes: 2

Related Questions