环境:
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 程序。