I have an SNS topic and an HTTP subscription, that call a Spring boot endpoint, my endpoint looks like this:
@RestController
public class MyController {
@PostMapping(value = "/path")
public ResponseEntity<Void> action(@RequestBody Object body) {
return ResponseEntity.noContent().build();
}
}
I try to call this endpoint when I publish a message in this topic using AWS CLI like this:
aws sns publish --topic-arn arn:aws:sns:eu-west-1:000000000000:MyTopic
--message '{"name": "test"}' --endpoint-url http://localhost:4566
but I got an error:
Unsupported Media Type: Content type 'text/plain;charset=UTF-8' is not supported
I add MediaType.TEXT_PLAIN_VALUE
to my code:
@PostMapping(value = "/path", consumes = MediaType.TEXT_PLAIN_VALUE)
But I got the same error.
For some reason, the topic sends the message using a different text/plain
media type.
I learned this documentation, and effectively the message is send using:
...
Content-Type: text/plain; charset=UTF-8
...
My question is there any way to force the SNS publisher to use application/json
?
Or is there any way to solve the issue in my controller?
2
Answers
The problem solved when I used
MediaType.TEXT_PLAIN_VALUE
and@RequestBody String body
Amazon SNS now supports custom
Content-Type
headers for HTTP messages delivered from topics. Here’s the announcement: https://aws.amazon.com/about-aws/whats-new/2023/03/amazon-sns-content-type-request-headers-http-s-notifications/You simply have to modify the
DeliveryPolicy
attribute of your Amazon SNS subscription, setting theheaderContentType
property toapplication/json
,application/xml
, or any other value supported. You can find all values supported here: https://docs.aws.amazon.com/sns/latest/dg/sns-message-delivery-retries.html#creating-delivery-policyYou set the
DeliveryPolicy
attribute by calling theSetSubscriptionAttributes
API action: https://docs.aws.amazon.com/sns/latest/api/API_SetSubscriptionAttributes.html