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
37.38k stars 5.06k forks source link

SDK NODE.JS: Get with body is not http standard #4617

Closed valerionarcisi closed 3 days ago

valerionarcisi commented 1 month ago

Self Checks

Dify version

2.3.1

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce

If you are using a base url some platform hosted in gcloud (for example) all GETs request returns 400 Bad Request.

✔️ Expected Behavior

Get should be whitout body

❌ Actual Behavior

Get Requests return 400 malformed request in some scenario like gcloud

dosubot[bot] commented 1 month ago

To address the issue of GET requests returning a 400 Bad Request error due to a body being included, you can modify the sendRequest method to ensure that the data field is explicitly set to null for GET requests. This will prevent sending a body with GET requests and avoid the 400 Bad Request errors on platforms like Google Cloud.

Here is how you can modify the sendRequest method in sdks/nodejs-client/index.js:

async sendRequest(
    method,
    endpoint,
    data = null,
    params = null,
    stream = false,
    headerParams = {}
) {
    const headers = {
        ...{
            Authorization: `Bearer ${this.apiKey}`,
            "Content-Type": "application/json",
        },
        ...headerParams
    };

    const url = `${this.baseUrl}${endpoint}`;
    let response;
    if (stream) {
        response = await axios({
            method,
            url,
            data: method === 'GET' ? null : data,  // Ensure data is null for GET requests
            params,
            headers,
            responseType: "stream",
        });
    } else {
        response = await axios({
            method,
            url,
            data: method === 'GET' ? null : data,  // Ensure data is null for GET requests
            params,
            headers,
            responseType: "json",
        });
    }

    return response;
}

This modification ensures that the data field is set to null when the method is GET, preventing a body from being sent with GET requests.

Additionally, you can refer to the test case in sdks/nodejs-client/index.test.js to verify that GET requests are handled correctly:

it('should make a successful request to the application parameter', async () => {
    const method = 'GET'
    const endpoint = routes.application.url
    const expectedResponse = { data: 'response' }
    axios.mockResolvedValue(expectedResponse)

    await difyClient.sendRequest(method, endpoint)

    expect(axios).toHaveBeenCalledWith({
      method,
      url: `${BASE_URL}${endpoint}`,
      data: null,  // Ensure data is null for GET requests
      params: null,
      headers: {
        Authorization: `Bearer ${difyClient.apiKey}`,
        'Content-Type': 'application/json',
      },
      responseType: 'json',
    })
})

This test case demonstrates that the data field is set to null for GET requests, ensuring that no body is sent.

By making these changes, you can avoid the 400 Bad Request errors caused by sending a body with GET requests on platforms like Google Cloud.

#### Details **Sources** - [sdks/nodejs-client/index.test.js](https://github.com/langgenius/dify/blob/main/sdks/nodejs-client/index.test.js) - [web/service/base.ts](https://github.com/langgenius/dify/blob/main/web/service/base.ts) - [sdks/nodejs-client/index.js](https://github.com/langgenius/dify/blob/main/sdks/nodejs-client/index.js) - [docker/docker-compose.middleware.yaml](https://github.com/langgenius/dify/blob/main/docker/docker-compose.middleware.yaml) - [docker/docker-compose.yaml](https://github.com/langgenius/dify/blob/main/docker/docker-compose.yaml)
**About Dosu** This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.