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

**URL:** <https://discourse.julialang.org/t/how-get-username-from-headers-in-amqpclient-with-using-rabbitmq/26793>\
**Category:** General Usage\
**Tags:** question, package, documentation\
**Created:** [July 25, 2019, 1:33pm UTC](https://discourse.julialang.org/t/how-get-username-from-headers-in-amqpclient-with-using-rabbitmq/26793 "2019-07-25T13:33:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Florek](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Martin\_Florek](https://discourse.julialang.org/u/Martin_Florek)\
**Post date:** [July 25, 2019, 1:33pm UTC](https://discourse.julialang.org/t/how-get-username-from-headers-in-amqpclient-with-using-rabbitmq/26793/1 "2019-07-25T13:33:11Z")

</div>

All or @tanmaykm I’m using [AMQPClient](https://github.com/JuliaComputing/AMQPClient.jl) 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](mailto: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?

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [July 26, 2019, 8:23pm UTC](https://discourse.julialang.org/t/how-get-username-from-headers-in-amqpclient-with-using-rabbitmq/26793/2 "2019-07-26T20:23:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Martin\_Florek](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Martin\_Florek](https://discourse.julialang.org/u/Martin_Florek)\
**Post date:** [July 29, 2019, 8:21pm UTC](https://discourse.julialang.org/t/how-get-username-from-headers-in-amqpclient-with-using-rabbitmq/26793/3 "2019-07-29T20:21:09Z")

</div>

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

```julia
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 ☹ .)  
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`.

---

<div class="post-metadata">

**Author:** ![Martin\_Florek](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Martin\_Florek](https://discourse.julialang.org/u/Martin_Florek)\
**Post date:** [August 8, 2019, 12:27pm UTC](https://discourse.julialang.org/t/how-get-username-from-headers-in-amqpclient-with-using-rabbitmq/26793/4 "2019-08-08T12:27:20Z")

</div>

Finally, the simple `copy` function helped me.

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

---

<div class="post-metadata">

**Author:** ![vsaase](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vsaase/32/11303_2.png) [@vsaase](https://discourse.julialang.org/u/vsaase)\
**Post date:** [September 26, 2019, 1:39pm UTC](https://discourse.julialang.org/t/how-get-username-from-headers-in-amqpclient-with-using-rabbitmq/26793/5 "2019-09-26T13:39:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Martin\_Florek](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Martin\_Florek](https://discourse.julialang.org/u/Martin_Florek)\
**Post date:** [January 10, 2020, 4:30pm UTC](https://discourse.julialang.org/t/how-get-username-from-headers-in-amqpclient-with-using-rabbitmq/26793/6 "2020-01-10T16:30:21Z")

</div>

@vsaase, final implementation takes the form of:

```julia
"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

```

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [January 11, 2020, 7:25am UTC](https://discourse.julialang.org/t/how-get-username-from-headers-in-amqpclient-with-using-rabbitmq/26793/7 "2020-01-11T07:25:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Martin\_Florek](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Martin\_Florek](https://discourse.julialang.org/u/Martin_Florek)\
**Post date:** [January 14, 2020, 6:15pm UTC](https://discourse.julialang.org/t/how-get-username-from-headers-in-amqpclient-with-using-rabbitmq/26793/8 "2020-01-14T18:15:13Z")

</div>

@Skoffer see

> help?\> String

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

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [January 14, 2020, 7:15pm UTC](https://discourse.julialang.org/t/how-get-username-from-headers-in-amqpclient-with-using-rabbitmq/26793/9 "2020-01-14T19:15:50Z")

</div>

Thank you, it’s useful.
