使用 okhttp 3.9.0 将文件上传到 REST 给出错误代码 = 422,消息 = Uprocessable 实体
Uploading File with okhttp 3.9.0 to REST is giving Error code=422, message=Uprocessable Entity
我正在尝试通过 okhttp3 (3.9.0) 将文件上传到 REST 服务。
它不起作用,我收到错误:**代码=422,消息=不可处理的实体**
但我找不到我的错误...
这是我的代码:
private void test_OK_HTTP() {
String userCredentials = "username:password";
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));
File f = new File("C:\history48.png");
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("DocumentName", "file.png")
.addFormDataPart("FK_Person", "1d64b9cc-d405-47c4-9adb-ef276c391ae0&")
.addFormDataPart("FK_FileManagerFormKey", "33")
.addFormDataPart("SystemFileType", "368")
.addFormDataPart("Subject", "test")
.addFormDataPart("SubjectDate", "2022-02-24")
.addFormDataPart("DocumentContent", "file.png", RequestBody.create(MediaType.parse("image/png"), f))
.build();
Request request = new Request.Builder()
.url("http://myurlthatworksfine/RestServiceTest/AddNewDocument")
.addHeader("api-version", "v1")
.addHeader("Authorization", basicAuth)
.post(requestBody)
.build();
System.out.println("Request: "+request.body().toString());
try {
Response response = client.newCall(request).execute();
System.out.println("Response: " + response.toString());
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error: "+e);
}
}
有人知道我做错了什么吗?
谢谢。
我的 test-id 中有一个简单的错误...现在可以使用...:)
错误:
.addFormDataPart("FK_Person", "1d64b9cc-d405-47c4-9adb-ef276c391ae0&")
好的:
.addFormDataPart("FK_Person", "1d64b9cc-d405-47c4-9adb-ef276c391ae0")
我正在尝试通过 okhttp3 (3.9.0) 将文件上传到 REST 服务。
它不起作用,我收到错误:**代码=422,消息=不可处理的实体** 但我找不到我的错误...
这是我的代码:
private void test_OK_HTTP() {
String userCredentials = "username:password";
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));
File f = new File("C:\history48.png");
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("DocumentName", "file.png")
.addFormDataPart("FK_Person", "1d64b9cc-d405-47c4-9adb-ef276c391ae0&")
.addFormDataPart("FK_FileManagerFormKey", "33")
.addFormDataPart("SystemFileType", "368")
.addFormDataPart("Subject", "test")
.addFormDataPart("SubjectDate", "2022-02-24")
.addFormDataPart("DocumentContent", "file.png", RequestBody.create(MediaType.parse("image/png"), f))
.build();
Request request = new Request.Builder()
.url("http://myurlthatworksfine/RestServiceTest/AddNewDocument")
.addHeader("api-version", "v1")
.addHeader("Authorization", basicAuth)
.post(requestBody)
.build();
System.out.println("Request: "+request.body().toString());
try {
Response response = client.newCall(request).execute();
System.out.println("Response: " + response.toString());
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error: "+e);
}
}
有人知道我做错了什么吗?
谢谢。
我的 test-id 中有一个简单的错误...现在可以使用...:)
错误: .addFormDataPart("FK_Person", "1d64b9cc-d405-47c4-9adb-ef276c391ae0&")
好的: .addFormDataPart("FK_Person", "1d64b9cc-d405-47c4-9adb-ef276c391ae0")