Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ - Send a JSON message

Tags:

rabbitmq

I send the following message with content type application/json:

enter image description here

However whene i get messages from the same RabbitMQ Web console, it shows the payload as String.

enter image description here

What am I doing wrong? Or am I fundamentally misunderstanding and the Payload is always of type String?

like image 770
userMod2 Avatar asked Apr 12 '18 04:04

userMod2


People also ask

Can we send JSON in RabbitMQ?

In this example a message containing sample sales data in JSON is received through an HTTP endpoint. This message is then converted to a string using a transformer and then sent to RabbitMQ using the AMQP connector. Once this message reaches the queue, it can be viewed throught he RabbitMQ web console.

Which annotation will make the class to send and receive JSON?

Send JSON Data in POST Spring provides a straightforward way to send JSON data via POST requests. The built-in @RequestBody annotation can automatically deserialize the JSON data encapsulated in the request body into a particular model object.

How do I get RabbitMQ data?

In the Managed Service for ClickHouse cluster, create a table on the RabbitMQ engine. Send the test data to the RabbitMQ queue. Check that the test data is present in the Managed Service for ClickHouse cluster table. Delete the resources you created.


2 Answers

From the official docs:

AMQP messages also have a payload (the data that they carry), which AMQP brokers treat as an opaque byte array. The broker will not inspect or modify the payload. It is possible for messages to contain only attributes and no payload. It is common to use serialisation formats like JSON, Thrift, Protocol Buffers and MessagePack to serialize structured data in order to publish it as the message payload. AMQP peers typically use the "content-type" and "content-encoding" fields to communicate this information, but this is by convention only.

So basically, RabbitMQ has no knowledge on JSON, messages all are just byte arrays to it

like image 113
Alex Buyny Avatar answered Sep 19 '22 11:09

Alex Buyny


from here

Content Type and Encoding

The content (MIME media) type and content encoding fields allow publishers communicate how message payload should be deserialized and decoded by consumers.

RabbitMQ does not validate or use these fields, it exists for applications and plugins to use and interpret.

by the way, using the rabbitMQ web gui, you use the words content_type, however in code (javascript confirmed), you use the key name contentType. it's a subtle difference, but enough to drive you crazy.

like image 27
jaxkewl Avatar answered Sep 21 '22 11:09

jaxkewl