AWS SDK(バージョン:2.17.201)を使用してAPIGatewayにリクエストを送信しようとしています。
しかし、ヘッダーに「user-agent」がないため、400エラーが発生します。
書き方が悪いのか、インフラの問題なのか、何かご存じの方教えて欲しいです。
コード
Java
public ApiGatewayResponse execute(String apiUrl) { // create header Map<String, List<String>> headers = new HashMap<>(); headers.put("Content-type", new ArrayList<String>(Arrays.asList("application/json"))); headers.put("x-api-key", new ArrayList<String>(Arrays.asList("xxx"))); headers.put("x-amz-security-token", new ArrayList<String>Arrays.asList("xxx"))); // ★Without this header I get a 400 error★ // headers.put("User-Agent", new ArrayList<String>(Arrays.asList("something"))); // create Request SdkHttpFullRequest httpFullRequest = SdkHttpFullRequest .builder() .uri(endPoint) .encodedPath(BASE_REQUEST_PATH + apiUrl) .headers(headers) .method(SdkHttpMethod.GET) .build(); // sign Aws4Signer signer = Aws4Signer.create(); Aws4SignerParams aws4SignerParams = Aws4SignerParams.builder() .awsCredentials( AwsBasicCredentials.create( properties.getAccessKey(), properties.getSecretAccessKey())) .signingRegion(Region.of(properties.getRegion())) .signingName(properties.getServiceName()) .build(); SdkHttpFullRequest signedRequest = signer.sign(httpFullRequest, aws4SignerParams); // create Request HttpExecuteRequest request = HttpExecuteRequest.builder() .request(signedRequest) .contentStreamProvider(signedRequest.contentStreamProvider().orElse(null)) .build(); // set Proxy SdkHttpClient httpClient = ApacheHttpClient .builder() .proxyConfiguration(ProxyConfiguration.builder().endpoint(proxyEndpoint).build()) .build(); // call request HttpExecuteResponse response; try { response = httpClient .prepareRequest(request) .call(); System.out.println("【Request】headers"); System.out.println(JSONUtil.toJSON(request.httpRequest().headers())); System.out.println("【Response】BODY"); System.out.println(IoUtils.toUtf8String(response.responseBody().get())); } catch (IOException e) { // error handling.. }
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
builder.setRequestExecutor(new HttpRequestExecutor()) // SDK handles decompression .disableContentCompression() .setKeepAliveStrategy(buildKeepAliveStrategy(standardOptions)) .disableRedirectHandling() .disableAutomaticRetries() .setUserAgent("") // SDK will set the user agent header in the pipeline. Don't let Apache waste time .setConnectionReuseStrategy(new SdkConnectionReuseStrategy()) .setConnectionManager(ClientConnectionManagerFactory.wrap(cm));
まだ回答がついていません
会員登録して回答してみよう