AWS SQS是否支持以JSON格式发送完整HTTPS POST请求?求示例
Great question! Let's break this down clearly:
Core Answer
AWS SQS's REST API does not support sending the entire request (including Action, Version, and other top-level parameters) as a JSON object in the request body. The official REST endpoint expects requests to use application/x-www-form-urlencoded formatting for these top-level API parameters, which is why your first test worked but the pure JSON request threw an error.
Why Your JSON Request Failed
When you sent the pure JSON body, SQS's endpoint couldn't parse the required Action parameter (since it was looking for URL-encoded form data, not JSON). Without identifying the Action (in this case SendMessage), the service couldn't determine which operation to authorize, hence the <AccessDeniedException> with the message "Unable to determine service/operation name to be authorized".
How to Work with JSON in SQS Requests
If you want to send JSON content via SQS, you have two valid approaches:
1. Embed JSON in the MessageBody (URL-encoded request)
This is the method you mentioned that worked. You can wrap your JSON content as the value of the MessageBody parameter, keeping the rest of the request in URL-encoded format. For example in Postman:
Action=SendMessage&MessageBody={"key":"value","anotherKey":"anotherValue"}&Expires=2020-10-15T12%3A00%3A00Z&Version=2012-11-05
Just make sure to properly URL-encode the JSON string if it contains special characters.
2. Use an AWS SDK (Recommended)
AWS SDKs (for Python, JavaScript, Java, etc.) abstract away the raw HTTP request formatting entirely. You can construct requests using native JSON/object structures, and the SDK will handle converting them to the proper URL-encoded format for SQS.
Here's a quick Python example using boto3:
import boto3 import json sqs = boto3.client('sqs', region_name='your-region') # Define your request parameters as a Python dictionary (which acts like JSON) send_message_params = { 'QueueUrl': 'your-queue-url', 'MessageBody': json.dumps({"key": "value", "message": "Hello from JSON"}), # Add other parameters like DelaySeconds, MessageAttributes if needed } response = sqs.send_message(**send_message_params) print(response)
This approach is cleaner, handles authentication automatically, and avoids manual formatting errors.
Final Note
While SQS doesn't support top-level JSON request bodies for its REST API, you can still send JSON content as the message payload using the methods above. The official documentation focuses on URL-encoded requests because that's the expected format for the raw REST endpoint.
内容的提问来源于stack exchange,提问作者WarSame

