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

其他-C#和Azure CosmosDB REST API:如何使用HTTPClient.PostAsync执行存储过程?

(其他 - C# and Azure CosmosDB REST API: how to execute stored procedure using HTTPClient.PostAsync?)

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

我正在尝试使用Azure CosmosDB REST API执行存储过程。根据此文档,我需要传递一个包含存储的proc输入参数的“数组”。

就我而言,我需要传递一个参数,它是一个对象。我已将该对象序列化,但是我不知道如何将其作为HTTPContent传递到PostAsync方法中。我应该使用哪种确切的HTTPContent类型以及将对象转换为哪种类型以便创建HTTPContent?

当我使用Postman执行此SP时,我的请求正文如下所示:

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

因此,我需要的是从JSON构造这种类型的主体并将其作为HTTPContent在C#中传递。

从提供的示例中,我可以得出结论,它是字符串数组。因此,我需要序列化JSON,将其添加到数组中,然后将其转换为HTTPContent。在“ StringContent”的情况下,应如何将字符串数组转换为字符串?这不起作用:

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

我从SP中收到JSON解析错误。

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

事实证明,一直以来,答案都在这里:CosmosDB API需要特定的Content-Type,显然,存储过程也是如此。