How to send JSON payload to RabbitMQ using the web plugin?

点点圈 提交于 2019-12-04 17:48:39

问题


I have a RabbitMQ 3.4.2 instance with a web management plugin installed.

When I push to the message {'operationId': 194} to the queue using Python's kombu queue package, the message is read on the other end as a dictionary.

However, when I send the message using the web console:

I get the following error on the receiving end:

operation_id = payload['operationId']
TypeError: string indices must be integers

I have tried adding a content-type header and property, with no success.

Since the reader code is the same, it means that the web sender does not mark the sent message as a JSON / dictionary payload, and therefore it is read as a string on the other end.

Any idea how to mark a message as a JSON message using the RabbitMQ web console?


回答1:


I had to use content_type instead of content-type (an underscore instead of a hyphen).

This is a pretty questionable design decision, because the standard everybody knows is content-type.




回答2:


You need to de-serialize the output.

import json
payload = json.loads(payload)
operation_id = payload['operationId']

In addition {'operationId': 194} is not valid JSON. Although it looks like you use double quotes in the screenshot, but make sure you replace the single quotes with double quotes.

Edit: So you are correct, kombu should handle this. Looking at the code it's likely that the header is case-sensitive. Change the properties header from Content-Type to content-type.



来源:https://stackoverflow.com/questions/34200756/how-to-send-json-payload-to-rabbitmq-using-the-web-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!