Search code examples
scalaamazon-web-servicesamazon-elasticsearchaws-java-sdk

Bad Request on AWS ElasticSearch


I'm trying to connect to an IAM controlled ElasticSearch domain, I've created a request, and signed it, and everything works fine for method GET, but on method POST I get a 400 Bad Request

This clearly has something to do with the payload. If I provide a payload empty string ("") it works apporpriately, but anything else results in a bad request.

What am I missing?

val url = s"https://$host/TEST/article/_search"
val serviceName = "es"
val regionName = "us-east-1"


val request = new DefaultRequest(serviceName)

val payload =
  """{"1":"1"}""".trim
val payloadBytes = payload.getBytes(StandardCharsets.UTF_8)
val payloadStream = new ByteArrayInputStream(payloadBytes)
request.setContent(payloadStream)

val endpointUri = URI.create(url)
request.setEndpoint(endpointUri)
request.setHttpMethod(HttpMethodName.POST)

val credProvider = new EnvironmentVariableCredentialsProvider
val credentials = credProvider.getCredentials

val signer = new AWS4Signer

signer.setRegionName(regionName)
signer.setServiceName(serviceName)

signer.sign(request, credentials)

val context = new ExecutionContext(true)

val clientConfiguration = new ClientConfiguration()
val client = new AmazonHttpClient(clientConfiguration)

val rh = new MyHttpResponseHandler
val eh = new MyErrorHandler

val response =
  client.execute(request, rh , eh, context);

Solution

  • note: if you run into this problem, inspect the actual content of the response, it may be a result of a mismatch between the index and your query.

    My problem was that the specific query I was using was inappropriate for the specified index, and that resulted in a 400