[英]Unable to execute multipart/form-data request using a file with jersey 2. Error 400
我可以在 Postman 中很好地执行这个请求。
POST /rest/v13/migrationPackages/actions/import HTTP/1.1
Authorization: Basic myEncodedCredentials
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: 70cd77f6-f3f0-48de-9411-b0cc48c9b206
Host: myhost.com
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: multipart/form-data; boundary=--------------------------522141775093976253434021
Content-Length: 5757
----------------------------522141775093976253434021
Content-Disposition: form-data; name="file"; filename="TestPackage1_5.zip"
<TestPackage1_5.zip>
----------------------------522141775093976253434021--
HTTP/1.1 202 Accepted
Date: Thu, 04 Nov 2021 16:27:26 GMT
Server: Unknown
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-ORACLE-DMS-RID: 0:1
X-Content-Type-Options: nosniff
Set-Cookie: JSESSIONID=d0dc3448f22742bbb6110683601f877d4af38565153e6fe771d1!-780730370; path=/; SameSite=None; secure; HttpOnly
X-ORACLE-DMS-ECID: 005nz^0^AfJ8xkWzLwyGOA0000^r00000l
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/json
{"taskId":37301093,"links":[{"rel":"related","href":"https://myhost.com/rest/v13/tasks/37301093"}]}
在 cUrl 中也可以正常工作:
curl -L -X POST 'https://myhost.com/rest/v13/migrationPackages/actions/import' -H 'Authorization: Basic myEncodedCredentials' -H 'Cookie: JSESSIONID=d0dc3448f22742bbb6110683601f877d4af38565153e6fe771d1!-780730370' -F 'file=@"/C:/temp/fdplugins/work/1/1/artifacts/Project1/Project1/TestPackage1_5.zip"'
我有使用 REST API 执行导入的用例( https://docs.oracle.com/en/cloud/saas/configure-price-quote/cxcpq/op-rest-v13-migrationpackages-actions-import-post。 html ),我将此代码与球衣一起使用来执行请求。
FlexRESTClient restClient = getClient();
restClient.path("/rest/v13/migrationPackages/actions/import");
final File fileToUpload = new File("C:/temp/fdplugins/work/1/1/artifacts/Project1/Project1/TestPackage1_5.zip");
final FileDataBodyPart part = new FileDataBodyPart("file", fileToUpload);
FormDataMultiPart form = new FormDataMultiPart();
form.field(part.getName(), part.getEntity(), part.getMediaType());
form.getField(part.getName()).setContentDisposition(FormDataContentDisposition.name(part.getName()).fileName(part.getContentDisposition().getFileName()).build());
Entity<FormDataMultiPart> entity = Entity.entity(form, MediaType.MULTIPART_FORM_DATA);
FlexRESTClientResponse clientResponse = restClient.setValidateResponse(true).mediatype(MediaType.APPLICATION_JSON).post(entity);
完整的 400 错误消息:
{"o:errorDetails":[{"Instance":"InvalidRequestParam","title":"Required parameter file missing.","o:errorPath":"file"},{"Instance":"InvalidRequestParam","title":"Unsupported param TestPackage1_5.zip.","o:errorPath":"TestPackage1_5.zip"}],"type":"HTTP://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html","title":"One or more parameters are invalid."}
我在这里缺少什么?
在打印从 Jersey 服务器发送的整个请求正文/实体后,我自己弄明白了。
这是来自我的 java 代码的 400 错误请求:
1 > POST https://myhost.com/rest/v13/migrationPackages/actions/import
1 > Accept: */*
1 > Accept-Encoding: gzip, deflate, br,deflate,gzip,x-gzip
1 > Authorization: Basic myEncodedCredential
1 > Cache-Control: no-cache
1 > Connection: keep-alive
1 > Content-Length: 5768
1 > Content-Type: multipart/form-data
1 > User-Agent: PostmanRuntime/7.28.4
--Boundary_1_1859374258_1636049106906
Content-Type: application/octet-stream
Content-Disposition: form-data; filename="TestPackage1_3.zip"; name="file"
[file binary]
--Boundary_1_1859374258_1636049106906--
Thu Nov 04 12:54:47 CDT 2021 - 1 - FINE - org.glassfish.jersey.logging.LoggingInterceptor.log - 1 * Client response received on thread main
1 < 400
1 < Connection: close
1 < Content-Encoding: gzip
1 < Content-Type: application/json
1 < Date: Thu, 04 Nov 2021 17:54:47 GMT
1 < Server: Unknown
1 < Set-Cookie: JSESSIONID=d2ba701938044361b33371d6213a3156411385263652bc4c065b!-780730370; path=/; SameSite=None; secure; HttpOnly
1 < Strict-Transport-Security: max-age=31536000; includeSubDomains
1 < Transfer-Encoding: chunked
1 < Vary: Accept-Encoding,User-Agent
1 < X-Content-Type-Options: nosniff
1 < X-ORACLE-DMS-ECID: 005nzcsu3128xkWzLwyGOA0000^r00000B
1 < X-ORACLE-DMS-RID: 0:1
Thu Nov 04 12:54:47 CDT 2021 - 1 - SEVERE - flexagon.ff.common.core.rest.FlexRESTClientResponse.init() - The REST call received error code [400].
[InboundJaxrsResponse{context=ClientResponse{method=POST, uri=https://myhost.com/rest/v13/migrationPackages/actions/import, status=400, reason=Bad Request}}]
Thu Nov 04 12:54:47 CDT 2021 - 1 - FINEST - flexagon.ff.common.core.rest.FlexRESTClient.post - Exiting [FlexRESTClientResponse-400]
{"o:errorDetails":[{"Instance":"InvalidRequestParam","title":"Required parameter file missing.","o:errorPath":"file"},{"Instance":"InvalidRequestParam","title":"Unsupported param TestPackage1_3.zip.","o:errorPath":"TestPackage1_3.zip"}],"type":HTTP://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html,"title":"One or more parameters are invalid."}
这是 Postman 发送的成功请求。 注意顺序差异内容配置:
POST /rest/v13/migrationPackages/actions/import HTTP/1.1
Authorization: Basic myEncodedCredentials
User-Agent: PostmanRuntime/7.28.4
Accept: */*
Cache-Control: no-cache
Postman-Token: 9f37ed85-9173-4735-9235-9722a41fc818
Host: myhost.com
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: multipart/form-data; boundary=--------------------------658555600649463253125207
Cookie: JSESSIONID=3c01da3b8b8548b081ca913bac958b074bccaedd860999f4e61b!-780730370
Content-Length: 5768
----------------------------658555600649463253125207
Content-Disposition: form-data; name="file"; filename="TestPackage1_3.zip"
Content-Type: application/octet-stream
<TestPackage1_3.zip>
----------------------------658555600649463253125207--
HTTP/1.1 202 Accepted
Date: Thu, 04 Nov 2021 17:53:03 GMT
Server: Unknown
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-ORACLE-DMS-RID: 0:1
X-Content-Type-Options: nosniff
Set-Cookie: JSESSIONID=1b70a9cd4f6d4e078b94e78cf068c1a14ee7a6573965ca226568!-780730370; path=/; SameSite=None; secure; HttpOnly
X-ORACLE-DMS-ECID: 005nzcmivdh8xkWzLwyGOA0000^r000016
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/json
这是工作代码:
FlexRESTClient restClient = getClient();
restClient.path("/rest/v13/migrationPackages/actions/import");
FileDataBodyPart fileUploadPart = new FileDataBodyPart();
fileUploadPart.setName("file");
fileUploadPart.setFileEntity(new File("C:/temp/fdplugins/work/1/1/artifacts/Project1/Project1/TestPackage1_5.zip"), MediaType.APPLICATION_OCTET_STREAM_TYPE);
FormDataMultiPart form = new FormDataMultiPart();
form.bodyPart(fileUploadPart);
form.getField("file").setContentDisposition(new FormDataContentDisposition("form-data", "file", "TestPackage1_5.zip", null, null, null, -1)
{
@Override
public String toString()
{
return "form-data; name=\"file\"; filename=\"TestPackage1_5.zip\"";
}
});
FlexRESTClientResponse clientResponse = restClient.setValidateResponse(true).mediatype(MediaType.APPLICATION_JSON).post(Entity.entity(form, MediaType.MULTIPART_FORM_DATA));
问题是内容配置中的字段顺序。 所以我手动设置了这些并且请求通过了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.