AWS SDK(バージョン:2.17.201)を使用してAPIGatewayにリクエストを送信しようとしています。
しかし、ヘッダーに「user-agent」がないため、400エラーが発生します。
書き方が悪いのか、インフラの問題なのか、何かご存じの方教えて欲しいです。
コード
Java
1public ApiGatewayResponse execute(String apiUrl) { 2 // create header 3 Map<String, List<String>> headers = new HashMap<>(); 4 headers.put("Content-type", new ArrayList<String>(Arrays.asList("application/json"))); 5 headers.put("x-api-key", new ArrayList<String>(Arrays.asList("xxx"))); 6 headers.put("x-amz-security-token", new ArrayList<String>Arrays.asList("xxx"))); 7 8 // ★Without this header I get a 400 error★ 9 // headers.put("User-Agent", new ArrayList<String>(Arrays.asList("something"))); 10 11 // create Request 12 SdkHttpFullRequest httpFullRequest = SdkHttpFullRequest 13 .builder() 14 .uri(endPoint) 15 .encodedPath(BASE_REQUEST_PATH + apiUrl) 16 .headers(headers) 17 .method(SdkHttpMethod.GET) 18 .build(); 19 20 // sign 21 Aws4Signer signer = Aws4Signer.create(); 22 Aws4SignerParams aws4SignerParams = Aws4SignerParams.builder() 23 .awsCredentials( 24 AwsBasicCredentials.create( 25 properties.getAccessKey(), 26 properties.getSecretAccessKey())) 27 .signingRegion(Region.of(properties.getRegion())) 28 .signingName(properties.getServiceName()) 29 .build(); 30 SdkHttpFullRequest signedRequest = signer.sign(httpFullRequest, aws4SignerParams); 31 32 // create Request 33 HttpExecuteRequest request = 34 HttpExecuteRequest.builder() 35 .request(signedRequest) 36 .contentStreamProvider(signedRequest.contentStreamProvider().orElse(null)) 37 .build(); 38 39 // set Proxy 40 SdkHttpClient httpClient = ApacheHttpClient 41 .builder() 42 .proxyConfiguration(ProxyConfiguration.builder().endpoint(proxyEndpoint).build()) 43 .build(); 44 45 // call request 46 HttpExecuteResponse response; 47 try { 48 response = httpClient 49 .prepareRequest(request) 50 .call(); 51 System.out.println("【Request】headers"); 52 System.out.println(JSONUtil.toJSON(request.httpRequest().headers())); 53 System.out.println("【Response】BODY"); 54 System.out.println(IoUtils.toUtf8String(response.responseBody().get())); 55 } catch (IOException e) { 56 // error handling.. 57 }
400エラーの内容
【Request】headers {Authorization":["AWS4-HMAC-SHA256 Credential=xxx, SignedHeaders=content-type;host;x-amz-date;x-amz-security-token;x-api-key, Signature=xxx"], Content-type":["application/json;charset=UTF-8"], Host":["xxx"], X-Amz-Date":["20220609T075444Z"], x-amz-security-token":["xxx"], x-api-key":["xxx"] } 【Response】BODY <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML4.01//EN""http://www.w3.org/TR/html4/strict.dtd"> <HTML> <HEAD> <TITLE>Bad Request</TITLE> <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"> </HEAD> <BODY> <h2>Bad Request - Invalid Header</h2> <hr> <p>HTTP Error 400. The request has an invalid header name.</p> </BODY> </HTML>
ApacheHttpClientのソースを見ると、意図的に消しているっぽいです。
software.amazon.awssdk.http.apache.ApacheHttpClient 153rows
Java
1builder.setRequestExecutor(new HttpRequestExecutor()) 2 // SDK handles decompression 3 .disableContentCompression() 4 .setKeepAliveStrategy(buildKeepAliveStrategy(standardOptions)) 5 .disableRedirectHandling() 6 .disableAutomaticRetries() 7 .setUserAgent("") // SDK will set the user agent header in the pipeline. Don't let Apache waste time 8 .setConnectionReuseStrategy(new SdkConnectionReuseStrategy()) 9 .setConnectionManager(ClientConnectionManagerFactory.wrap(cm));
回答1件