Команда CURL
curl -k --user automation:abcd@1234 -X POST https://test.xyz.com:8444/api/certificates/import -H "Content-type: multipart/mixed" -H "Accept: application/json" -F "myPartName1=@JSON_Data.txt;type=application/json" -F "myPartName2=@SSHKEY.PUB;type=application/octet-stream"
Вышеуказанная команда работает нормально (200 ОК) и может получить ответ в терминале.
Ожидание:
Требуется вызвать вышеCURL после вызова в приложении Java (класс POJO или с пружинными зависимостями загрузки)
Код Java попытался
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
HttpPost httpPostCall = new HttpPost("https://test.xyz.com:8444/api/certificates/import");
httpPostCall.setHeader(HttpHeaders.AUTHORIZATION, "Basic ACCDFbhdGlvbjphYmNkQDEyMzQ==");
httpPostCall.setHeader(HttpHeaders.CONTENT_TYPE, "multipart/mixed");
httpPostCall.setHeader(HttpHeaders.ACCEPT, "application/json");
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
File jsonDataFile = org.springframework.util.ResourceUtils.getFile("classpath:testdata/JSON_Data.txt");
File sshkeyFile = org.springframework.util.ResourceUtils.getFile("classpath:testdata/SSHKEY.PUB");
multipartEntity.addPart("myPartName1", new FileBody(jsonDataFile, ContentType.APPLICATION_JSON));
multipartEntity.addPart("myPartName2", new FileBody(sshkeyFile, ContentType.APPLICATION_OCTET_STREAM));
HttpEntity multipart = multipartEntity.build();
httpPostCall.setEntity(multipart);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpPostCall);
return response;
Вывод
400 Плохой запрос
Примечание: Скопировал те же два входных файла, которые существуют в команде curl в папке ресурсов java
Пожалуйста, поделитесь своими мыслями, почему я получаю неверный запрос?
Трассировка:
Read : true
exec : true
19:11:04.818 [main] DEBUG org.apache.http.client.protocol.RequestAddCookies - CookieSpec selected: default
19:11:04.836 [main] DEBUG org.apache.http.client.protocol.RequestAuthCache - Auth cache not set in the context
19:11:04.839 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection request: [route: {s}->https://test.xyz.com:8444][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
19:11:04.854 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {s}->test.xyz.com:8444][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
19:11:04.857 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Opening connection {s}->test.xyz.com:8444
19:11:05.154 [main] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connecting to test.xyz.com/10.142.242.18:8444
19:11:05.154 [main] DEBUG org.apache.http.conn.ssl.SSLConnectionSocketFactory - Connecting socket to test.xyz.com/10.142.242.18:8444 with timeout 0
19:11:05.545 [main] DEBUG org.apache.http.conn.ssl.SSLConnectionSocketFactory - Enabled protocols: [TLSv1, TLSv1.1, TLSv1.2]
19:11:05.550 [main] DEBUG org.apache.http.conn.ssl.SSLConnectionSocketFactory - Enabled cipher suites:[TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, TLS_RSA_WITH_AES_256_CBC_SHA256, TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384, TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384, TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, TLS_DHE_DSS_WITH_AES_256_CBC_SHA256, TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, TLS_RSA_WITH_AES_256_CBC_SHA, TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDH_RSA_WITH_AES_256_CBC_SHA, TLS_DHE_RSA_WITH_AES_256_CBC_SHA, TLS_DHE_DSS_WITH_AES_256_CBC_SHA, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, TLS_RSA_WITH_AES_128_CBC_SHA256, TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256, TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, TLS_DHE_DSS_WITH_AES_128_CBC_SHA256, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_DSS_WITH_AES_128_CBC_SHA, TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, TLS_RSA_WITH_AES_256_GCM_SHA384, TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384, TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, TLS_DHE_DSS_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_DSS_WITH_AES_128_GCM_SHA256, TLS_EMPTY_RENEGOTIATION_INFO_SCSV]
19:11:05.550 [main] DEBUG org.apache.http.conn.ssl.SSLConnectionSocketFactory - Starting handshake
19:11:06.238 [main] DEBUG org.apache.http.conn.ssl.SSLConnectionSocketFactory - Secure session established
19:11:06.238 [main] DEBUG org.apache.http.conn.ssl.SSLConnectionSocketFactory - negotiated protocol: TLSv1.2
19:11:06.239 [main] DEBUG org.apache.http.conn.ssl.SSLConnectionSocketFactory - negotiated cipher suite: TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
19:11:06.239 [main] DEBUG org.apache.http.conn.ssl.SSLConnectionSocketFactory - peer principal: CN=test.xyz.com, O=xyz USA L.P., L=Round Rock, ST=Texas, C=US
19:11:06.239 [main] DEBUG org.apache.http.conn.ssl.SSLConnectionSocketFactory - peer alternative names: [test.xyz.com]
19:11:06.240 [main] DEBUG org.apache.http.conn.ssl.SSLConnectionSocketFactory - issuer principal: CN=Entrust Certification Authority - L1K, OU="(c) 2012 Entrust, Inc. - for authorized use only", OU=See www.entrust.net/legal-terms, O="Entrust, Inc.", C=US
19:11:06.248 [main] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connection established 10.132.0.29:61129<->10.143.242.18:8444
19:11:06.249 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Executing request POST /api/certificates/import HTTP/1.1
19:11:06.249 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Proxy auth state: UNCHALLENGED
19:11:06.250 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> POST /api/certificates/import HTTP/1.1
19:11:06.250 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Authorization: Basic ACCDFbhdGlvbjphYmNkQDEyMzQ=
19:11:06.250 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Type: multipart/mixed
19:11:06.250 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Accept: application/json
19:11:06.250 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Length: 1102
19:11:06.250 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Host: test.xyz.com:8444
19:11:06.250 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Connection: Keep-Alive
19:11:06.250 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.6 (Java/1.8.0_181)
19:11:06.250 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Accept-Encoding: gzip,deflate
19:11:06.258 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "POST /api/v1.4/certificates/import HTTP/1.1[\r][\n]"
19:11:06.259 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Authorization: Basic YXV0b21hdGlvbjpEZWxsQDIwMTc=[\r][\n]"
19:11:06.259 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Type: multipart/mixed[\r][\n]"
19:11:06.259 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Accept: application/json[\r][\n]"
19:11:06.259 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Length: 1102[\r][\n]"
19:11:06.259 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Host: test.xyz.com:8444[\r][\n]"
19:11:06.259 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
19:11:06.260 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.6 (Java/1.8.0_181)[\r][\n]"
19:11:06.260 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
19:11:06.260 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
19:11:06.260 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "--kgELBKTzgQN8182tOfNdurUdazq4-ce34[\r][\n]"
19:11:06.261 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Disposition: form-data; name="myPartName1"; filename="JSON_Data.txt"[\r][\n]"
19:11:06.263 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Type: application/json; charset=UTF-8[\r][\n]"
19:11:06.263 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Transfer-Encoding: binary[\r][\n]"
**19:11:06.263 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
19:11:06.263 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "{[\r][\n]"
19:11:06.264 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[0x9]"name": "vxdfvd",[\r][\n]"
19:11:06.264 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[0x9]"subject": "test",[\r][\n]"
19:11:06.264 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[0x9]"type": "ssh",[\r][\n]"
19:11:06.264 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[0x9]"usage": "login",[\r][\n]"
19:11:06.264 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[0x9]"account": "testaccount",[\r][\n]"
19:11:06.264 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[0x9]"validityPeriod": "3650",[\r][\n]"
19:11:06.264 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[0x9]"caPassword": "testca"[\r][\n]"
19:11:06.264 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "}"
19:11:06.268 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"**
19:11:06.268 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "--kgELBKTzgQN8182tOfNdurUdazq4-ce34[\r][\n]"
19:11:06.268 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Disposition: form-data; name="myPartName2"; filename="SSHKEY.PUB"[\r][\n]"
19:11:06.268 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Type: application/octet-stream; charset=UTF-8[\r][\n]"
19:11:06.268 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Transfer-Encoding: binary[\r][\n]"
**19:11:06.268 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
19:11:06.269 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "---- BEGIN SSH2 PUBLIC KEY ----[\r][\n]"
19:11:06.269 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Comment: ""[\r][\n]"
19:11:06.269 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "P1A8942YOCmDOUkD5exmRpLfli6YiWTeUxQzmHyX8FTE/p1qk5NmF+CztlAtGvxWEuNZ1AbHx[\r][\n]"
19:11:06.269 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "ebkyx8RKKyhcW+InoP+2kL4kNWc1L1ibNOjpY8Ii/bzbuQmV/t5EoVfCA0Thjww+[\r][\n]"
19:11:06.269 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "WDpiKvBygbJ+YACVZNdZ7qdsSoM9OPpK22SA4ayV74QXQYBMQlPlsoQWfFozFeEn[\r][\n]"
19:11:06.269 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "nMactcGFbaQd7snBx+YACVZNdZ7qdsSoM9OPpK22SA4ayV74QXQYBMQlPlsoQWfFozFeEn[\r][\n]"
19:11:06.269 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "P1A8942YOCmDOUkD5exmRpLfli6YiWTeUxQzmHyX8FTE0kWwqZMq+9nnbbzZpUyvUOwaqEAyb1V10sylPqQbuh[\r][\n]"
19:11:06.269 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "YACVZNdZ7qdsSoM9OPpK22SA4ayV74QXQYBMQlPlsoQWfFozFeEn[\r][\n]"
19:11:06.269 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "---- END SSH2 PUBLIC KEY ----"
19:11:06.270 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
19:11:06.271 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "--kgELBKTzgQN8182tOfNdurUdazq4-ce34--[\r][\n]"**
19:11:07.120 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "HTTP/1.1 400 Bad Request[\r][\n]"
19:11:07.121 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Server: Apache-Coyote/1.1[\r][\n]"
19:11:07.121 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Set-Cookie: [\r][\n]"
19:11:07.121 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Strict-Transport-Security: max-age=15768000; includeSubDomains[\r][\n]"
19:11:07.121 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Content-Type: application/json[\r][\n]"
19:11:07.121 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Transfer-Encoding: chunked[\r][\n]"
19:11:07.121 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Date: Sun, 16 Dec 2018 13:41:07 GMT[\r][\n]"
19:11:07.121 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Connection: close[\r][\n]"
19:11:07.121 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "[\r][\n]"
19:11:07.121 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "3[\r][\n]"
19:11:07.121 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "{[\n]"
19:11:07.122 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "}[\r][\n]"
19:11:07.129 [main] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 **400 Bad Request**
19:11:07.129 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Server: Apache-Coyote/1.1
19:11:07.129 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Set-Cookie:
19:11:07.129 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Strict-Transport-Security: max-age=15768000; includeSubDomains
19:11:07.129 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Type: application/json
19:11:07.129 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Transfer-Encoding: chunked
19:11:07.129 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Date: Sun, 16 Dec 2018 13:41:07 GMT
19:11:07.129 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Connection: close
ResponseEntityProxy{[Content-Type: application/json,Chunked: true]}