Closed valerionarcisi closed 3 days 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.
To continue the conversation, mention @dosu.
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