apache / apache/apisix

request help: the format of response body that returned by plugin should match Content-Type

Open
#8,511 10 comments 1 reaction 1 assignee Claimed by @MekelCon View on GitHub
good first issue
Dominant language
Lua
Stars
17.1k
Forks
2.9k
Avg merge
3d 16h
Merged PRs (30d)
63

Description

**Step 1**
Create a route with consumer restriction
```
curl --location --request PUT ".../apisix/admin/routes/content-type" \
--header "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" \
--header "Content-Type: application/json" \
--data-raw "{
\"methods\": [
\"GET\"
],
\"plugins\": {
\"consumer-restriction\": {
\"whitelist\": [
\"nobody\"
]
}
},
\"uri\": \"/content-type/get\"
}"
```
**Step 2** Try to consume the route without being identified to raise an error
I said i want application/json response :
```
curl --location --request GET ".../content-type/get" \
--header "Accept: application/json"
```

Response is :
```
# http code
401
# body
{"message":"Missing authentication or identity verification."}
# In headers :
Content-Type: text/plain; charset=utf-8
```
The Content-Type header should be : application/json as it is in fact a Json in response.

This issue is not on "consumer-restriction"plugin only, it also happen for the key-auth plugin when you provide a wrong key, and i guess it is a .general plugin error behavior.
I think it should exist a general common way to respond error from plugin to allow manage right content-type depending the response format.
For example in case of a request like :
```
curl --location --request GET "..../content-type/get" \
--header "Accept: application/xml"
```
i expect a response like

```
# body
Missing authentication or identity verification.
# In headers :
Content-Type: application/xml; charset=utf-8
```
---
In case of unknow accept or not managed, here we go for text/plain :
```
--header "Accept: */*"
````
Response should be something like :
```
# body
Missing authentication or identity verification.
# In headers :
Content-Type: text/plain; charset=utf-8
```

_Originally posted by @MekelCon in https://github.com/apache/apisix/discussions/8504#discussioncomment-4386548_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.