RabbitMQ REST HTTP JSON payload

风格不统一 提交于 2020-12-08 06:32:28

问题


I am trying to use RabbitMQ HTTP REST client to publish messages into the queue. I am using the following url and request

http://xxxx/api/exchanges/xxxx/exc.notif/publish

{
 "routing_key":"routing.key",
  "payload":{

  },
 "payload_encoding":"string",
 "properties":{
   "headers":{
     "notif_d":"TEST",
     "notif_k": ["example1", "example2"],
     "userModTime":"timestamp"
   }
 }
}

And getting back from the rabbit the following response:

{"error":"bad_request","reason":"payload_not_string"}

I have just one header set:

Content-Type:application/json

I was trying to set the

"payload_encoding":"base64",

but it didn't help. I am new to rabbit any response is welcome.


回答1:


Try with

{
"properties": {
"content-type": "application/json"
},
"routing_key": "testKey",
"payload": "1234",
"payload_encoding": "string"
}



回答2:


Working example. We need simple to escape doublequotes. It is important that the colon is outside of the quotes, as this causes inexplicable errors.

{
    "properties": {},
    "routing_key": "q_testing",
    "payload": "{
        \"message\": \"message from terminal\"
    }",
    "payload_encoding": "string"
}



回答3:


I managed to send content-type using underscore "_" instead of dash.

See here for list of valid properties. See RabbitMQ Management HTTP API for some examples.

To publish a json message using curl to rabbit exchange:

curl -i -u guest:guest -XPOST --data '{"properties":\
{"content_type":"application/json"}, \
"routing_key":"", \
"payload":"{\"foo\":\"bar\"}",\
"payload_encoding":"string"}' \
"http://localhost:15672/api/exchanges/%2f/exchange_name/publish"

content_type is written using underscore, routing_key is empty to send a message to exchange, not to particular queue.




回答4:


To use a JSON formatted payload you have to encode it in base64 and use the "payload_encoding": "base64" attribute.



来源:https://stackoverflow.com/questions/44905126/rabbitmq-rest-http-json-payload

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