You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apisix.apache.org by jie123108 <ji...@163.com> on 2020/04/23 04:10:48 UTC

Shouldn't apisix provide a unified function to format the response body?

For different API business systems, the format of the returned json may be different, for example, in one system, the structure of the returned message is this: 
````
{ok: true|false, code: 'error code', data: {}}
````
Or it could be:
````
{status: 0, errmsg: 'error message', result: {}
````


One problem with using apisix is that apisix returns an error message like this: 
````
{error_msg: 'error message'}
````
In some of the plugins, the return is this:
````
{message: 'error message'}
````
Such an inconsistent return format can cause a lot of trouble for client development.




Shouldn't the apisix core library provide a uniform formatting function so that users can customize the return values.


In the example above, the user can customize it in this way:


````
function response_format(resp)
const json_response = {
ok = resp.error_msg == "",
code = resp.error_msg,
data = resp,
}
return json_response
end
````
This way, the error messages returned by the apisix system, become consistent with the business system. The client will only need to handle one message format.


Another problem to consider is that if we use apisix to handle html static website, the current system error return is also json, for the end user to return a json directly, is not very friendly? 
Then we have a unified formatting method, is it possible to consider the case of formatting to html.


jie123108@163.com

Re: Shouldn't apisix provide a unified function to format the response body?

Posted by YuanSheng Wang <me...@apache.org>.
````
{error_msg: 'error message'}
````

I think this style should be enough.

The `code` is meaningless, I do not like it.
The `code` is meaningless too.

most of case:

{ok = true}
{ok = false, error_msg = "xxxx"}

so we can drop `ok`, it is meaningless.


On Thu, Apr 23, 2020 at 12:11 PM jie123108 <ji...@163.com> wrote:

> For different API business systems, the format of the returned json may be
> different, for example, in one system, the structure of the returned
> message is this:
> ````
> {ok: true|false, code: 'error code', data: {}}
> ````
> Or it could be:
> ````
> {status: 0, errmsg: 'error message', result: {}
> ````
>
>
> One problem with using apisix is that apisix returns an error message like
> this:
> ````
> {error_msg: 'error message'}
> ````
> In some of the plugins, the return is this:
> ````
> {message: 'error message'}
> ````
> Such an inconsistent return format can cause a lot of trouble for client
> development.
>
>
>
>
> Shouldn't the apisix core library provide a uniform formatting function so
> that users can customize the return values.
>
>
> In the example above, the user can customize it in this way:
>
>
> ````
> function response_format(resp)
> const json_response = {
> ok = resp.error_msg == "",
> code = resp.error_msg,
> data = resp,
> }
> return json_response
> end
> ````
> This way, the error messages returned by the apisix system, become
> consistent with the business system. The client will only need to handle
> one message format.
>
>
> Another problem to consider is that if we use apisix to handle html static
> website, the current system error return is also json, for the end user to
> return a json directly, is not very friendly?
> Then we have a unified formatting method, is it possible to consider the
> case of formatting to html.
>
>
> jie123108@163.com



-- 
*MembPhis*
My github: https://github.com/membphis
Apache APISIX: https://github.com/apache/incubator-apisix