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

fix: DifyRequestException Request failed with status: 400 #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/main/java/ai/dify/javaclient/ChatClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ai.dify.javaclient;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import okhttp3.*;

Expand Down Expand Up @@ -50,7 +51,7 @@ private String generateQueryParams(Map<String, String> params) {
/**
* Creates a new chat message.
*
* @param inputs The chat message inputs.
* @param inputs The chat message inputs. Must be a valid JSON object str.
* @param query The query associated with the chat message.
* @param user The user associated with the chat message.
* @param stream Whether to use streaming response mode.
Expand All @@ -59,8 +60,12 @@ private String generateQueryParams(Map<String, String> params) {
* @throws DifyClientException If an error occurs while sending the request.
*/
public Response createChatMessage(String inputs, String query, String user, boolean stream, String conversation_id) throws DifyClientException {
assert JSON.isValidObject(inputs);
assert query != null && !query.isEmpty();
assert user != null ;

JSONObject json = new JSONObject();
json.put("inputs", inputs);
json.put("inputs", JSON.parseObject(inputs));
json.put("query", query);
json.put("user", user);
json.put("response_mode", stream ? "streaming" : "blocking");
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/ai/dify/javaclient/ChatClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void setUp() throws IOException {
public void testCreateChatMessage() throws Exception {
when(mockResponse.isSuccessful()).thenReturn(true);

chatClient.createChatMessage("testInputs", "testQuery", "testUser", true, "conversation123");
chatClient.createChatMessage("{}", "testQuery", "testUser", true, "conversation123");

verify(mockClient).newCall(any(Request.class));
verify(mockCall).execute();
Expand Down