Warm tip: This article is reproduced from serverfault.com, please click

C# and Azure CosmosDB REST API: how to execute stored procedure using HTTPClient.PostAsync?

发布于 2020-11-26 16:29:22

I'm trying to execute a stored procedure using Azure CosmosDB REST API. According to this document I need to pass an "array" containing stored proc input parameters.

In my case, I need to pass one parameter and it's an object. I have the object serialized but I can't figure out how to pass it as HTTPContent into PostAsync method. What exact type of HTTPContent I should use and what type I should convert my object to in order to create HTTPContent?

When I execute this SP using Postman, my request body looks like this:

["{\r\n\"id\" : \"someid\",\r\n\"data\": \"somedata\"\r\n}"]

so all I need is to construct this type of body from JSON and pass it as HTTPContent in C#.

From the provided example I could conclude that it's the array of strings. So I need to serialize my JSON, add it to the array and then convert it into HTTPContent. In case of "StringContent" how I should convert string array into string? This doesn't work:

string s = "[\"" + serialisedJson + "\"]";
HttpContent c = new StringContent(s, Encoding.UTF8, "application/json");

I'm getting JSON parsing error from the SP.

Questioner
Svetlana
Viewed
0
Svetlana 2020-11-30 19:33:34

It turned out, all this time the answer was here: CosmosDB API requires a specific Content-Type, which is apparently the case for stored procedures as well.