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

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

Open
4 tasks done
valerionarcisi opened this issue May 23, 2024 · 1 comment
Open
4 tasks done

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

valerionarcisi opened this issue May 23, 2024 · 1 comment
Labels
🐞 bug Something isn't working

Comments

@valerionarcisi
Copy link
Contributor

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • Please do not modify this template :) and fill in all the required fields.

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

Copy link

dosubot bot commented May 23, 2024

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


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.

@dosubot dosubot bot added the 🐞 bug Something isn't working label May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant