Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: As a user, I want to to use the body-transformer plugin for other data formats without warnings #10681

Closed
jesse-r-s-hines opened this issue Dec 20, 2023 · 6 comments · Fixed by #10862
Assignees

Comments

@jesse-r-s-hines
Copy link
Contributor

Description

The body-transformer plugin is a very flexible plugin to modify the body content of requests and responses, and we can already use the body-transformer plugin to manipulate arbitrary data formats with the lua templates. However, if you don't specify input_format and Content-Type isn't JSON or XML it will always log a warning about "no input format to parse request/response body".

Adding another format to the input_format enum, none, that explicitly tells body-transformer to not try to parse or validate the body content and silences this warning would be convenient.

@shreemaan-abhishek
Copy link
Contributor

would you like to contribute this?

@shreemaan-abhishek shreemaan-abhishek moved this to 📋 Backlog in Apache APISIX backlog Dec 27, 2023
@jesse-r-s-hines
Copy link
Contributor Author

Sorry for the late reply. I probably won't get time to work on this one myself.

@smileby
Copy link
Contributor

smileby commented Jan 4, 2024

@shreemaan-abhishek feel good, I'd like to try it if needed ☺

@smileby
Copy link
Contributor

smileby commented Jan 22, 2024

@jesse-r-s-hines @shreemaan-abhishek
Hi, Can you provide a more detailed idea?
I have two questions:

  • If no try to parse or validate are performed when set to none, does it need to process the template content?
  • input_format is not specified and the Content-Type is not JSON or XML, is there an example of the request that can be provided and what to expect after execution?

Or have a better idea

@jesse-r-s-hines
Copy link
Contributor Author

jesse-r-s-hines commented Jan 22, 2024

Currently, if you do not specify input_format and there's no Content-Type header the plugin will skip trying to parse the response body. This allows arbitrary transformations as it doesn't try to parse the body but still runs the template which can access the original body with _body.

The only problem is it spams warnings about "no input format to parse response body". I'd like an explicit "input_format": "none" to just silence that warning.

In our case we also have an upstream service that sometimes returns incorrect Content-Type headers, so it would be nice if "input_format": "none" caused it to not try to parse the content regardless of Content-Type and let the template logic do whatever is necessary to convert to the correct format. Currently if you don't strip Content-Type with response-rewrite the plugin will abort before calling the template if it can't parse the original response.

So something like:

Original response:

Content-Type: application/json
Body:
not actually json

Config:

"body-transformer": {
    "response": {
        "template": "{\"message\": \"{* string.gsub(_body, 'not ', '') *}\"}"
    }
}

Transformed response:

Content-Type: application/json
Body:
{"message": "actually json"}

Thanks!

@smileby
Copy link
Contributor

smileby commented Jan 23, 2024

@jesse-r-s-hines @shreemaan-abhishek
Hi, I submitted a PR to solve this issue. Please help confirm the correctness.

@shreemaan-abhishek shreemaan-abhishek moved this from 📋 Backlog to 👀 In review in Apache APISIX backlog Jan 23, 2024
@github-project-automation github-project-automation bot moved this from 👀 In review to ✅ Done in Apache APISIX backlog Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants