Azure-Samples / Azure-Samples/azure-sql-db-invoke-external-rest-endpoints
Sending to `ServiceBus` with `BrokerProperties` fails with `Bad Request`
- Dominant language
- Jupyter Notebook
- Stars
- 37
- Forks
- 16
- PR merge metrics
- No merged PRs in 30d
Description
When I run the following SQL script to send a `TestMessage` message to Azure Service Bus topic `{ServiceBusTopicName}`:
```sql
declare @headers nvarchar(max), @url nvarchar(max), @payload nvarchar(max), @response nvarchar(max);
set @headers = json_object(
'Accept': 'application/xml',
'Content-Type': 'application/json',
'BrokerProperties': '{"Label": "TestMessage", "SessionId": "Session1"}'
);
set @payload = json_object('MessageId': 1);
select @headers, @payload;
set @url = 'https://{ServiceBusName}.servicebus.windows.net/{ServiceBusTopicName}/messages'
exec sp_invoke_external_rest_endpoint
@url = @url,
@method = 'POST',
@headers = @headers,
@payload = @payload,
@credential = [https://{ServiceBusName}.servicebus.windows.net],
@response = @response output
select cast(@response as xml)
go
```
It fails with `400 Bad Request` pointing to `'BrokerProperties' is invalid`:
```xml
400
The value '{\"Label\": \"TestMessage\", \"SessionId\": \"Session1\"}' of the HTTP header 'BrokerProperties' is invalid. TrackingId:1030e3ab-5a69-4414-b28e-73af5a76888d_G58, SystemTracker:***, Timestamp:2023-10-05T13:32:19
```
However, once I remove `BrokerProperties` from the header, it works returning `201 Created`:
```xml
```
My best guess is `sp_invoke_external_rest_endpoint` does not correctly decode a value that contains encoded JSON
```json
{"Accept":"application\/xml","Content-Type":"application\/json","BrokerProperties":"{\"Label\": \"TestMessage\", \"SessionId\": \"Session1\"}"}
```
What am I doing wrong?
Do you have any workarounds?
It there better place to file this bug?
Contributor guide
Assessment
This issue has not been assessed yet.