You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2022/12/13 07:51:57 UTC

[GitHub] [apisix] tzssangglass opened a new issue, #8511: request help: the format of response body that returned by plugin should match Content-Type

tzssangglass opened a new issue, #8511:
URL: https://github.com/apache/apisix/issues/8511

         **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 
   <message>Missing authentication or identity verification.</message>
   # 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_
       


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] tokers commented on issue #8511: request help: the format of response body that returned by plugin should match Content-Type

Posted by GitBox <gi...@apache.org>.
tokers commented on issue #8511:
URL: https://github.com/apache/apisix/issues/8511#issuecomment-1348012286

   > @MekelCon thanks your feedback, I have confirmed this.
   > 
   > maybe this is a good first issue. cc @spacewander
   
   Maybe adding a field `error_message_type` in the `plugin._meta`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] tzssangglass commented on issue #8511: request help: the format of response body that returned by plugin should match Content-Type

Posted by GitBox <gi...@apache.org>.
tzssangglass commented on issue #8511:
URL: https://github.com/apache/apisix/issues/8511#issuecomment-1350241112

   > Maybe adding a field `error_message_type` in the `plugin._meta`.
   
   maybe fix in `core.response.exit()`?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] ro4i7 commented on issue #8511: request help: the format of response body that returned by plugin should match Content-Type

Posted by "ro4i7 (via GitHub)" <gi...@apache.org>.
ro4i7 commented on issue #8511:
URL: https://github.com/apache/apisix/issues/8511#issuecomment-1465231556

   Hello @spacewander @tzssangglass @tokers 
   
   if this issue is still open, please assign it to me and give feedback on my below solution: 
   
   To remove this issue is to modify the plugin code to set the `Content-Type` header based on the requested response format. This can be achieved by checking the `Accept` header in the incoming request and setting the `Content-Type` header in the outgoing response accordingly.
   
   For example, in the `consumer-restriction` plugin, we can modify the code to set the `Content-Type` header in the `access` function as follows:
   ```
   local function access(conf, ...)
     -- Check if the consumer is whitelisted
     local consumer_id = ngx.ctx.authenticated_consumer and ngx.ctx.authenticated_consumer.id
     if not consumer_id or not is_in_whitelist(consumer_id, conf.whitelist) then
       ngx.header["Content-Type"] = get_content_type()
       return kong.response.exit(401, { message = "Missing authentication or identity verification." })
     end
   end
   
   -- Helper function to get the Content-Type header based on the Accept header in the request
   local function get_content_type()
     local accept_header = ngx.req.get_headers()["Accept"]
     if accept_header then
       if string.find(accept_header, "application/json", 1, true) then
         return "application/json"
       elseif string.find(accept_header, "application/xml", 1, true) then
         return "application/xml"
       end
     end
     return "text/plain"
   end
   ```
   Similarly, we can modify the `key-auth` plugin or any other plugin to set the `Content-Type` header based on the requested response format. This can help ensure that the format of the response body matches the `Content-Type` header and prevent issues like this issue.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] spacewander commented on issue #8511: request help: the format of response body that returned by plugin should match Content-Type

Posted by GitBox <gi...@apache.org>.
spacewander commented on issue #8511:
URL: https://github.com/apache/apisix/issues/8511#issuecomment-1350491080

   `core.response.exit()` +1, use the right type according to the client's Accept header if possible.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] tzssangglass commented on issue #8511: request help: the format of response body that returned by plugin should match Content-Type

Posted by GitBox <gi...@apache.org>.
tzssangglass commented on issue #8511:
URL: https://github.com/apache/apisix/issues/8511#issuecomment-1347882928

   @MekelCon thanks your feedback, I have confirmed this.
   
   maybe this is a good first issue. cc @spacewander 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] MekelCon commented on issue #8511: request help: the format of response body that returned by plugin should match Content-Type

Posted by GitBox <gi...@apache.org>.
MekelCon commented on issue #8511:
URL: https://github.com/apache/apisix/issues/8511#issuecomment-1350512268

   I can try to do it i think 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] spacewander commented on issue #8511: request help: the format of response body that returned by plugin should match Content-Type

Posted by GitBox <gi...@apache.org>.
spacewander commented on issue #8511:
URL: https://github.com/apache/apisix/issues/8511#issuecomment-1352578383

   @MekelCon 
   Assign to you


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] simon-flury commented on issue #8511: request help: the format of response body that returned by plugin should match Content-Type

Posted by "simon-flury (via GitHub)" <gi...@apache.org>.
simon-flury commented on issue #8511:
URL: https://github.com/apache/apisix/issues/8511#issuecomment-1629217501

   What's about this issue? I have the same problem.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] spacewander commented on issue #8511: request help: the format of response body that returned by plugin should match Content-Type

Posted by "spacewander (via GitHub)" <gi...@apache.org>.
spacewander commented on issue #8511:
URL: https://github.com/apache/apisix/issues/8511#issuecomment-1465449394

   Solving it in each plugin is unacceptable. Please read what is voted by the maintainers:
   https://github.com/apache/apisix/issues/8511#issuecomment-1350241112
   https://github.com/apache/apisix/issues/8511#issuecomment-1350491080


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org