4

I want to send a messge to RabbitMQ in a dictionary format:

import pika

# ....
my_msg = {}
my_msg["a"] = 1
my_msg["a"]["b"] = 2
channel.basic_publish(exchange="", routing_key="some_key", body=my_msg)

And an error I get:

TypeError: unhashable type: 'slice'

Note that I have plenty of my_msg and each of them has a few keys, so I need somehow to be able to send a list dictionaries to RabbitMQ.

How can I do that? Or are there other options?

Alan Coromano
  • 24,958
  • 53
  • 135
  • 205

2 Answers2

8

You need to serialize your dictionaries into strings and send them over RabbitMQ.

See this question

Community
  • 1
  • 1
Konstantin
  • 24,271
  • 5
  • 48
  • 65
3

According to the documentation, body should be a string.

You might try body=json.dumps(my_msg)

Ben Graham
  • 2,089
  • 17
  • 21