Problem
Publishing a message to a Cloud Pub/Sub topic through an API call with cURL fails with the following error:
error code: 400 Bad Request error message: Invalid JSON payload received... Cannot bind query parameter... Field could not be found in request message status: INVALID_ARGUMENT
Environment
- Cloud Pub/Sub
Solution
- The message data has to be passed as a base64 encoded string. Use the following commands to base64 encode a message and pass it with the --data field in the cURL command:
message='demo message'
encoded_message='echo $message | base64'
curl -s -X POST -H 'content-type: application/json' -H "Authorization: Bearer $TOKEN" 'https://pubsub.googleapis.com/v1/projects/<project name>/topics/<topic name>:publish' --data "{\"messages\" : [{\"data\": \"$encoded_message\"}]}"
Cause
To publish a message using REST API or cURL commands, a POST request is sent. In the request, the body includes:
- KEY: the key of a message attribute
- VALUE: the value for the key of the message attribute
- MESSAGE_DATA: a base64-encoded string with the message data
The message must contain either a non-empty data field or at least one attribute, and must be a base64-encoded string.