【发布时间】:2021-12-18 21:26:45
【问题描述】:
我可以在 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."}
我在这里错过了什么?
【问题讨论】:
-
只需使用
form.bodyPart(part)。如果无法从文件扩展名中自动检测到内容类型,您可能还需要指定媒体类型。不确定。 -
我确实尝试过同样的问题
标签: java maven jersey jersey-2.0