RabbitMQ REST HTTP JSON有效负载

xqk2d5yq  于 2023-05-22  发布在  RabbitMQ
关注(0)|答案(5)|浏览(147)

我尝试使用RabbitMQ HTTP REST客户端将消息发布到队列中。我正在使用以下URL和请求
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"
   }
 }
}

从兔子那里得到了如下的回答:

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

我只有一个header set:

Content-Type:application/json

我想把

"payload_encoding":"base64",

但没有用我是新来的兔子任何回应都是受欢迎的。

bt1cpqcv

bt1cpqcv1#

试试用

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

xjreopfe2#

工作示例。我们需要simple来转义双引号。重要的是冒号在引号之外,因为这会导致无法解释的错误。

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

d6kp6zgx3#

我设法使用下划线“_”而不是破折号发送content-type。
有关有效属性的列表,请参见此处。参见RabbitMQ Management HTTP API以获得一些示例。
使用curl向rabbit exchange发布json消息:

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使用下划线编写,routing_key为空,用于将消息发送到exchange,而不是特定队列。

qij5mzcb

qij5mzcb4#

要使用JSON格式的payload,您必须使用base64对其进行编码并使用"payload_encoding": "base64"属性。

798qvoo8

798qvoo85#

一种解决方案是将payload_encoding设置为base64。虽然上面已经回答了这个问题,但下面是一个完整的curl示例:

json='{"type": "earl-grey", "strength": 7, "milk": true}'
amqp_host=amqp.improbability.cloud
amqp_username=slarti
amqp_password=bartfast
amqp_vhost=heart-of-gold
amqp_exchange=food.dispenser
amqp_queue=tea-requests
amqp_message_id=$(uuid)
amqp_delivery_mode=2
payload=$(echo -n $json | base64 -w0)
authorization=$(echo -n "${amqp_username}:${amqp_password}" | base64 -w0)

curl https://${amqp_host}/api/exchanges/${amqp_vhost}/${amqp_exchange}/publish   \
  -H "authorization: Basic ${authorization}" \
  -H 'Content-Type: application/json;charset=UTF-8' \
  --data @- <<EOF
{
  "properties": {
    "message_id":"${amqp_message_id}",
    "delivery_mode": ${amqp_delivery_mode}
  },
  "routing_key": "${amqp_queue}",
  "payload_encoding":"base64",
  "payload": "${payload}"
}
EOF

上面的渲染与shell变量展开:

curl https://amqp.improbability.cloud/api/exchanges/heart-of-gold/food.dispenser/publish 
  -H "authorization: Basic c2xhcnRpOmJhcnRmYXN0" \
  -H "Content-Type: application/json;charset=UTF-8" \
  --data @- <<EOF
{
  "properties": {
    "message_id":"eb28ad00-f86f-11ed-9e7f-00155d591867",
    "delivery_mode": 2
  },
  "routing_key": "tea-requests",
  "payload_encoding":"base64",
  "payload": "eyJ0eXBlIjogImVhcmwtZ3JleSIsICJzdHJlbmd0aCI6IDcsICJtaWxrIjogdHJ1ZX0="
}
EOF

结果:

=> {"routed":true}

相关问题