尝试从我的vert.x(客户端)发送一个文档和一些数据到django rest-framework(服务器端):代码服务器端为:
@api_view(['POST'])
@parser_classes([MultiPartParser, FormParser])
def quick_indexing(request):
data = request.data
print(data) # print the entire request data dictionary
test_value = data.get("test")
if test_value:
return Response(f"test attribute value: {test_value}")
else:
return Response("test attribute not found")
代码客户端:
Vertx vertx = Vertx.vertx();
WebClient client = WebClient.create(vertx);
Buffer file = vertx.fileSystem().readFileBlocking("C:/Users/houth/CIN Hassan.pdf");
MultipartForm form = MultipartForm.create()
.attribute("test", "succed")
.binaryFileUpload("file", "filename", file, "Application/pdf");
// Send a POST request with the file and form data
client.postAbs("http://localhost:8000/quick-indexing")
.putHeader(String.valueOf(HttpHeaders.CONTENT_TYPE), "multipart/form-data")
.sendMultipartForm(form,
ar -> {
if (ar.succeeded()) {
// Handle the response
HttpResponse<Buffer> response = ar.result();
System.out.println("Status code: " + response.statusCode());
System.out.println("Response body: " + response.bodyAsString());
} else {
// Handle the failure
Throwable cause = ar.cause();
System.err.println("Request failed: " + cause.getMessage());
}
});
System.out.println("end");
---〉客户端的结果是:
end
Status code: 200
Response body: "test attribute not found"
[ERROR] 2023-04-08 01:39:22.881 invalid version format: <!DOCTYPE
java.lang.IllegalArgumentException: invalid version format: <!DOCTYPE
at io.netty.handler.codec.http.HttpVersion.<init>(HttpVersion.java:116) ~[netty-codec-http-4.1.86.Final.jar:4.1.86.Final]
at io.netty.handler.codec.http.HttpVersion.valueOf(HttpVersion.java:78) ~[netty-codec-http-4.1.86.Final.jar:4.1.86.Final]
---〉服务器端的结果为:
<QueryDict: {}>
[08/Apr/2023 01:39:22] "POST /quick-indexing HTTP/1.1" 200 26
[08/Apr/2023 01:39:22] code 400, message Bad request syntax ('9d')
[08/Apr/2023 01:39:22] "9d" 400 -
----〉问题是,如果我从Postman调用API,它会成功
CDL
将数据和文件从vert.x webclient发送到API rest框架
1条答案
按热度按时间sg24os4d1#
您确保从vert. X发送的表单不是空的吗