How get username from :headers in AMQPClient with using RabbitMQ?

All or @tanmaykm I’m using AMQPClient and I would like get user name from message.
When I run commands

gChannel = channel(conn, AMQPClient.UNUSED_CHANNEL, true)
msg = basic_get(gChannel, “rQueue”, false)

I get

Message(UInt8[0x7b, 0x22, 0x64, 0x61, 0x74, 0x61, 0x22, 0x3a, 0x5b, 0x7b … 0x73, 0x22, 0x3a, 0x33, 0x36, 0x30, 0x30, 0x7d, 0x7d, 0x7d], Dict{Symbol,Union{TAMQPBit, TAMQPFieldTable, TAMQPLongStr, TAMQPShortStr, Integer}}(:headers=>FieldTable
username => test@example.com,:content_type=>application/json,:correlation_id=>9f890e3e-3586-434e-99e6-1adbf9729e8f,:priority=>0x00,:delivery_mode=>0x02,:content_encoding=>UTF-8), 1684, “”, 1, true, “yop”, “add_rQueue”, 1)

I would like get user name test@example.com probably from FieldTable but If I run get method return me answer:

julia> gHeaders = get(msg.properties, :headers, “”)
FieldTable
=>

How can I get username from this message?

I think you want to call headers(msg) to get the Dict of headers for your rabbit message. Unless your data is in the message data, then you want to access msg.data, but that just returns a byte array (Vector{UInt8}). For me, my rabbit message data is JSON, so I do JSON3.read(msg.data) in order to parse the message data into something I can use.

@quinnj or @tanmaykm yes exactly, I would like get values of headers e.g. username. Type of :headers is AMQPClient.TAMQPFieldTable.
I got

julia> gHeaders
FieldTable
    username =>  test@example.com

juliia> gHeaders
FieldTable
     => 

I would like log username as string and used it in Message for response queue. Now I don’t know how? (I noticed detail that after the second call to the FieldTable, the FieldTable of gHeaders is empty :frowning: .)
My data in the message data is fine. For me, my message data is JSON, too. I use JSON.parse(String(msg.data)) together with JSONSchema.isvalid.

Finally, the simple copy function helped me.

global gHeaders = get(msg.properties, :headers, “”)
logMsg("username: " * String(copy(gHeaders.data[1].val.fld.data)))

there must be a better way…
headers(msg) and headers(msg.properties) do not work

@vsaase, final implementation takes the form of:

"Return AMQP messsage headers as dictionary."
function extractHeaders(msg::AMQPClient.Message)
  d = Dict()
  for x ∈ msg.properties[:headers].data
    key = String(copy(x.name.data))
    value = String(copy(x.val.fld.data))
    d[key] = value
  end
  return d
end

Correct me if I am wrong, but it looks like copy is unnecessary here, because String should allocate anyway.

@Skoffer see

help?> String

To avoid truncation use String(copy(v)).

1 Like

Thank you, it’s useful.