0

I am trying to pass a list of Messages to Service Bus via the Python SDK and I get a 400 response as JSON is not in required format.

I am doing something like this and it fails:

messages = [Message({'id':1,'name':'bob'}),Message({'id':2,'name':'bill'})]
sb.send_queue_message_batch('queue_name', messages)

If I do this it works:

messages = [Message('bob'),Message('bill')]
sb.send_queue_message_batch('queue_name', messages)

Or if I call send_queue_message individually like this it works

sb.send_queue_message('queue_name', Message({'id':1,'name':'bob'}))
sb.send_queue_message('queue_name', Message({'id':2,'name':'bill'}))

Looking at the source it calls a method on the message to create the format expected in a batch so not sure what I should be doing differently. Unfortunately all examples of batch I can find are the simple string approach.

The consumer at the other end will be a .Net app so I need to ensure it can still be deserialised. I could call json.dump on the message content and pass it as a stringified version of the body but that does not sound like the ideal solution.

Thanks

SDK Source for batch: https://github.com/Azure/azure-sdk-for-python/blob/587bc9a2f955f43c67b02c537521f31aa4c27555/azure-servicebus/azure/servicebus/servicebusservice.py#L892

likeaninja
  • 65
  • 5
  • Could you please post the exception details ? – Thomas Jan 28 '18 at 20:54
  • The response from the service bus rest endpoint is bad request 400. Json is not in required format. I don't think there is an exception in the SDK that I can see. I think it is something in the serialisation of the messages for batch operations that doesn't seem to work if you pass in an object rather than a string for the message body. – likeaninja Jan 29 '18 at 07:28
  • Just had a another look in the SDK and the call to get the body as required for batch just returns body decoded as utf8 with a comment that it has to be a string. I'll try encoding the message body so the SDK can decode it and see if that works – likeaninja Jan 29 '18 at 07:43
  • This seems to work, although I don't think this should be how to interact with the SDK: `messages = list(map(lambda x: Message(json.dumps(x)), commands)) self.service_bus.send_queue_message_batch(self.queue_name, messages)` – likeaninja Jan 29 '18 at 10:06

1 Answers1

0

Yo will be able to send any object using the ServiceBusMessage in v7 of servicebus https://pypi.org/project/azure-servicebus/7.0.0/

import os
from azure.servicebus import ServiceBusClient, ServiceBusMessage


CONNECTION_STR = os.environ['SERVICE_BUS_CONNECTION_STR']
QUEUE_NAME = os.environ["SERVICE_BUS_QUEUE_NAME"]

servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONNECTION_STR, logging_enable=True)
with servicebus_client:
    sender = servicebus_client.get_queue_sender(queue_name=QUEUE_NAME)
    with sender:
      sender.send_messages([ServiceBusMessage(object1), ServiceBusMessage(object2)])
rakshith91
  • 682
  • 5
  • 13