環境:
vllm 0.8.5
java 17
Qwen3-32B-FP8
問題描述:
JAVA請求vllm的api服務報錯Unsupported upgrade request、 Invalid HTTP request received.
WARNING: Unsupported upgrade request.
INFO: - "POST /v1/chat/completions HTTP/1.1" 400 Bad Request
WARNING: Invalid HTTP request received.
服務端:
解決方案:
1.原來請求腳本
import okhttp3.*;public class VllmApiDemo (public static void main(String[] args) throws Exception {OkHttpClient client = new OkHttpClient();String json = "{"+ "\"model\": \"Qwen3-32B-FP8\","+ "\"messages\": [{\"role\": \"user\", \"content\": \"你好\")]"+ "}";RequestBody body = RequestBody.create(json, MediaType.parse("application/json"));Request request = new Request.Builder().url("http://192.168.16.2:8700/v1/chat/completions").post(body).build();Response response = client.newCall(request).execute();System.out.println(response.body().string());}
}
錯誤日志分析:
WARNING: Unsupported upgrade request.
INFO: - "POST /v1/chat/completions HTTP/1.1" 400 Bad Request
WARNING: Invalid HTTP request received.
服務端提示請求格式有誤,導致 400 錯誤。 Java 代碼,有幾處明顯的語法和格式問題,可能導致請求體JSON格式不正確,服務端無法解析:
1.代碼語法錯誤
- 第一行
mport okhttp3.*;
應為import okhttp3.*;
- 類定義括號用錯了,應該是
{
而不是(
- JSON 字符串中,
messages
數組結尾用了)]
,應為}]
- JSON 字符串最后的引號關閉處格式有問題
2. 正確的 JSON 請求體應為
{"model": "Qwen3-32B-FP8","messages": [{"role": "user","content": "你好"}]
}
3. 修正后的 Java 示例代碼
import okhttp3.*;public class VllmApiDemo {public static void main(String[] args) throws Exception {OkHttpClient client = new OkHttpClient();String json = "{"+ "\"model\": \"Qwen3-32B-FP8\","+ "\"messages\": [{\"role\": \"user\", \"content\": \"你好\"}]"+ "}";RequestBody body = RequestBody.create(json, MediaType.parse("application/json"));Request request = new Request.Builder().url("http://192.168.16.2:8700/v1/chat/completions").post(body).build();Response response = client.newCall(request).execute();System.out.println(response.body().string());}
}
4. 重點說明
- JSON格式正確,數組和對象括號匹配
- 類定義和import語句正確
- 請求地址和模型名稱確認無誤
- 請求體內容用
MediaType.parse("application/json")
指定媒體類型
4. 建議調試步驟
- 你可以先用
curl
命令測試接口,確認服務端正常響應,例如:
curl -X POST http://192.168.16.2:8700/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"model":"Qwen3-32B-FP8","messages":[{"role":"user","content":"你好"}]}'
- 確認
curl
成功返回后,再運行 Java 程序。