dify icon indicating copy to clipboard operation
dify copied to clipboard

fix: when http request body is raw or json, if there are line breaks and double quotes, the request will fail

Open hellof20 opened this issue 1 year ago • 0 comments

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.

image image

I found that the error was caused by multi-line text or double quotes image

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) and cd 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

hellof20 avatar Jul 10 '24 05:07 hellof20

hello @iamjoel , could you help to review this?

hellof20 avatar Jul 15 '24 11:07 hellof20

@Yeuoly Please help to review it.

iamjoel avatar Jul 15 '24 11:07 iamjoel

@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

hellof20 avatar Jul 16 '24 00:07 hellof20

@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.

Yeuoly avatar Jul 16 '24 03:07 Yeuoly

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?

onevcat avatar Aug 05 '24 06:08 onevcat