fix: when http request body is raw or json, if there are line breaks and double quotes, the request will fail
Description
My workflow is to send the output of the large model to an external API via http request node,when http request body is raw or json, if there are line breaks and double quotes, the request will fail.
I found that the error was caused by multi-line text or double quotes
Checklist:
[!IMPORTANT]
Please review the checklist below before submitting your pull request.
- [ ] Please open an issue before creating a PR or link to an existing issue
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I ran
dev/reformat(backend) andcd web && npx lint-staged(frontend) to appease the lint gods
Fixes
Type of Change
- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] This change requires a documentation update, included: Dify Document
- [ ] Improvement, including but not limited to code refactoring, performance optimization, and UI/UX improvement
- [ ] Dependency upgrade
hello @iamjoel , could you help to review this?
@Yeuoly Please help to review it.
@Yeuoly I tried to modify it in http_request_executor.py, but when the request parameter is raw or json, it is multi-line,
{
"msg_type":"text",
"content":{
"text": " {"xxx"}
}
}
If it is replaced with '\n', it will also cause the http request parameter format to be incorrect and cause an error. In addition, I found that there was no problem with the LLM node output, and the problem occurred after being processed by variable_template_parser, so I modified variable_template_parser
@Yeuoly I tried to modify it in http_request_executor.py, but when the request parameter is raw or json, it is multi-line,
{ "msg_type":"text", "content":{ "text": " {"xxx"} } }If it is replaced with '\n', it will also cause the http request parameter format to be incorrect and cause an error. In addition, I found that there was no problem with the LLM node output, and the problem occurred after being processed by variable_template_parser, so I modified variable_template_parser
Actually, I guess what we need is escaping quotes within plaintext in a JSON field, I examined the code and discovered that line breaks are not handled correctly here https://github.com/langgenius/dify/blob/main/api/core/workflow/nodes/http_request/http_executor.py#L347 the original logic is checking if it's already a valid JSON string, if so, escape quotes, otherwise do nothing, maybe you can optimize this.
We are also facing the same problem and seems there is no way to create a correct JSON request for API use. It becomes a blocker for our use of Dify.
Is there any workaround or plan to fix?