langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
44.47k stars 6.23k forks source link

http-request-tool prompts JSON format error in workflow #6684

Closed greycodee closed 4 weeks ago

greycodee commented 1 month ago

Self Checks

Dify version

0.6.15

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

workflow

Xnipstep-1

Xnip2024-07-25_23-30-38

✔️ Expected Behavior

Ability to successfully request

❌ Actual Behavior

In the Http request step, a json format error occurs.

Xnip2024-07-25_23-27-25

But when running on a separate node, it works fine.

Xnip2024-07-25_23-27-58

dosubot[bot] commented 1 month ago

Hi @greycodee! I'm here to help you with bugs, questions, and becoming a contributor. Let's solve this together!

To resolve the JSON format error in your workflow when using the http-request-tool to send a POST request to http://my_flask_container:5000/post, ensure that the JSON body is correctly formatted. Here is an example of how you can structure the JSON body for the POST request:

request_body = {
    'point': APIBasedExtensionPoint.APP_EXTERNAL_DATA_TOOL_QUERY.value,
    'params': {
        'app_id': app_model.id,
        'tool_variable': tool_variable,
        'inputs': inputs,
        'query': '{{#sys.query#}}' if app_model.mode == AppMode.CHAT.value else ''
    }
}

request_body_json = json.dumps(request_body)
request_body_json = request_body_json.replace(r'\{\{', '{{').replace(r'\}\}', '}}')

http_request_node = {
    "id": f"http_request_{index}",
    "position": None,
    "data": {
        "title": f"HTTP REQUEST {api_based_extension.name}",
        "type": NodeType.HTTP_REQUEST.value,
        "method": "post",
        "url": "http://my_flask_container:5000/post",
        "authorization": {
            "type": "api-key",
            "config": {
                "type": "bearer",
                "api_key": api_key
            }
        },
        "headers": {
            "Content-Type": "application/json"
        },
        "params": "",
        "body": {
            "type": "json",
            "data": request_body_json
        }
    }
}

Ensure that the request_body_json is correctly formatted and does not contain any escape characters that could cause a JSON format error. Additionally, make sure that the Content-Type header is set to application/json if it is not already set, as shown in the HttpExecutor class:

if node_data.body.type == 'json' and not content_type_is_set:
    self.headers['Content-Type'] = 'application/json'

This ensures that the server correctly interprets the body of the request as JSON. Also, make sure that the inputs parameter in your request body is a dictionary and not another data type, as required by the WorkflowRunApi class in your Flask application:

parser = reqparse.RequestParser()
parser.add_argument('inputs', type=dict, required=True, nullable=False, location='json')
parser.add_argument('files', type=list, required=False, location='json')
args = parser.parse_args()

By ensuring that the inputs parameter is a dictionary and correctly formatted, you can avoid JSON format errors in your workflow [1][2][3][4].

To continue talking to Dosu, mention @dosu.