Skip to content

Commit d5e66da

Browse files
author
vesladm1
committed
Fix Chinese encoding issue in ChatGptController
1 parent 8e91a8c commit d5e66da

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import java.io.OutputStreamWriter;
77
import java.net.HttpURLConnection;
88
import java.net.URL;
9-
9+
import org.json.JSONObject;
10+
import org.json.JSONException;
11+
import org.json.JSONArray;
12+
import org.wise.portal.dao.ObjectNotFoundException;
1013
import org.springframework.beans.factory.annotation.Autowired;
1114
import org.springframework.core.env.Environment;
1215
import org.springframework.security.access.annotation.Secured;
@@ -32,17 +35,19 @@ protected String sendChatMessage(@RequestBody String body) {
3235
throw new RuntimeException("OPENAI_API_KEY is not set");
3336
}
3437
try {
38+
System.out.println("sendChatMessage from wise api server");
3539
URL url = new URL("https://api.openai.com/v1/chat/completions");
3640
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
3741
connection.setRequestMethod("POST");
3842
connection.setRequestProperty("Authorization", "Bearer " + openaiApiKey);
39-
connection.setRequestProperty("Content-Type", "application/json");
43+
connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
44+
connection.setRequestProperty("Accept-Charset", "UTF-8");
4045
connection.setDoOutput(true);
4146
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
4247
writer.write(body);
4348
writer.flush();
4449
writer.close();
45-
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
50+
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(),"ISO-8859-1"));
4651
String line;
4752
StringBuffer response = new StringBuffer();
4853
while ((line = br.readLine()) != null) {

wise_api_local_changes.patch

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
diff --git a/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java b/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java
2+
index 9cfbd86..9cbc0bc 100644
3+
--- a/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java
4+
+++ b/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java
5+
@@ -6,7 +6,10 @@ import java.io.InputStreamReader;
6+
import java.io.OutputStreamWriter;
7+
import java.net.HttpURLConnection;
8+
import java.net.URL;
9+
-
10+
+import org.json.JSONObject;
11+
+import org.json.JSONException;
12+
+import org.json.JSONArray;
13+
+import org.wise.portal.dao.ObjectNotFoundException;
14+
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.core.env.Environment;
16+
import org.springframework.security.access.annotation.Secured;
17+
@@ -32,17 +35,19 @@ public class ChatGptcontroller {
18+
throw new RuntimeException("OPENAI_API_KEY is not set");
19+
}
20+
try {
21+
+ System.out.println("sendChatMessage from wise api server");
22+
URL url = new URL("https://api.openai.com/v1/chat/completions");
23+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
24+
connection.setRequestMethod("POST");
25+
connection.setRequestProperty("Authorization", "Bearer " + openaiApiKey);
26+
- connection.setRequestProperty("Content-Type", "application/json");
27+
+ connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
28+
+ connection.setRequestProperty("Accept-Charset", "UTF-8");
29+
connection.setDoOutput(true);
30+
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
31+
writer.write(body);
32+
writer.flush();
33+
writer.close();
34+
- BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
35+
+ BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(),"ISO-8859-1"));
36+
String line;
37+
StringBuffer response = new StringBuffer();
38+
while ((line = br.readLine()) != null) {

0 commit comments

Comments
 (0)