From fbc55b6956d310878ed7a82b9211b68749a0cdab Mon Sep 17 00:00:00 2001 From: Ohlicher Robert Date: Wed, 2 Aug 2023 18:07:55 +0200 Subject: [PATCH] use rabbitmq message's header values as metadata values in the binding #3030 Signed-off-by: Ohlicher Robert --- bindings/rabbitmq/rabbitmq.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/bindings/rabbitmq/rabbitmq.go b/bindings/rabbitmq/rabbitmq.go index e10f7422f3..d4ae6bb75a 100644 --- a/bindings/rabbitmq/rabbitmq.go +++ b/bindings/rabbitmq/rabbitmq.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "math" + "net/url" "reflect" "strconv" "sync" @@ -464,8 +465,20 @@ func (r *RabbitMQ) handleMessage(ctx context.Context, handler bindings.Handler, r.logger.Info("Input binding channel closed") return } + + metadata := make(map[string]string) + // Passthrough any custom metadata to the handler. + for k, v := range d.Headers { + if s, ok := v.(string); ok { + // Escape the key and value to ensure they are valid URL query parameters. + // This is necessary for them to be sent as HTTP Metadata. + metadata[url.QueryEscape(k)] = url.QueryEscape(s) + } + } + _, err := handler(ctx, &bindings.ReadResponse{ - Data: d.Body, + Data: d.Body, + Metadata: metadata, }) if err != nil { ch.Nack(d.DeliveryTag, false, true)