How to add a header key:value pair when publishing a message with pika

后端 未结 3 442
独厮守ぢ
独厮守ぢ 2021-01-12 02:48

I am writing an automated test to test a consumer. So far I did not need to include a header when publishing messages but now I do. And it seems like its lacking documentati

相关标签:
3条回答
  • 2021-01-12 03:18

    cant say where i get this, but i do it like:

    props = pika.BasicProperties({'headers': {'key': 'value'}})
    channel.basic_publish(exchange=self.exchange,
                              routing_key=self.routing_key,
                              body=message, properties = props)
    
    0 讨论(0)
  • 2021-01-12 03:20

    The official document was mentioned as follows:

    hdrs = {u'': u' ',
        u'': u'',
        u'': u''}
    properties = pika.BasicProperties(app_id='example-publisher',
        content_type='application/json',  
        headers=hdrs)
    
    0 讨论(0)
  • 2021-01-12 03:24

    You would use pika.BasicProperties to add headers.

    channel.basic_publish(exchange=self.exchange,
                          routing_key=self.routing_key,
                          properties=pika.BasicProperties(
                              headers={'key': 'value'} # Add a key/value header
                          ),
                          body=message)
    

    The official documentation for pika does indeed not cover this scenario exactly, but the documentation does have the specifications listed. I would would strongly recommend that you bookmark this page, if you are going to continue using pika.

    0 讨论(0)
提交回复
热议问题