skip to Main Content

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


  1. Chosen as BEST ANSWER

    The problem solved when I used MediaType.TEXT_PLAIN_VALUE and @RequestBody String body

    @PostMapping(value = "/path", consumes = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<String> action(@RequestBody String body) {
    

  2. 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 the headerContentType property to application/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-policy

    {
        "healthyRetryPolicy": {
            "minDelayTarget": 1,
            "maxDelayTarget": 60,
            "numRetries": 50,
            "numNoDelayRetries": 3,
            "numMinDelayRetries": 2,
            "numMaxDelayRetries": 35,
            "backoffFunction": "exponential"
        },
        "throttlePolicy": {
            "maxReceivesPerSecond": 10
        },
        "requestPolicy": {
            "headerContentType": "application/json"
        }
    }
    

    You set the DeliveryPolicy attribute by calling the SetSubscriptionAttributes API action: https://docs.aws.amazon.com/sns/latest/api/API_SetSubscriptionAttributes.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search