繁体   English   中英

如何在发布请求正文 (RestSharp) 中格式化和发布 Json

[英]How do I format and post Json in the post request body (RestSharp)

我不明白如何在请求正文中发送这个 json。 这是来自 postman 创建集合体的 json( https://www.postman.com/postman/workspace/postman-public-workspace/request/12959542-049042b8-447f-4f71-8b874f-ae97 ):

{
    "collection": {
        "info": {
            "name": "Sample Collection 909",
            "description": "This is just a sample collection.",
            "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
        },
        "item": [
            {
                "name": "This is a folder",
                "item": [
                    {
                        "name": "Sample POST Request",
                        "request": {
                            "url": "https://postman-echo.com/post",
                            "method": "POST",
                            "header": [
                                {
                                    "key": "Content-Type",
                                    "value": "application/json"
                                }
                            ],
                            "body": {
                                "mode": "raw",
                                "raw": "{\"data\": \"123\"}"
                            },
                            "description": "This is a sample POST Request"
                        }
                    }
                ]
            },
            {
                "name": "Sample GET Request",
                "request": {
                    "url": "https://postman-echo/get",
                    "method": "GET",
                    "description": "This is a sample GET Request"
                }
            }
        ]
    }
}

上面的 Json 使用 Postman 应用程序工作正常,我的尝试是:

RestSharp.Settings.Init("collections", Method.Post);
RestSharp.Settings.AddHeader("X-Api-Key", "I hid my key");
RestSharp.Settings.restRequest.RequestFormat = DataFormat.Json;

RestSharp.Settings.restRequest.AddParameter("application/json; charset=utf-8", "{\n    \"collection\": {\n        \"info\": {\n            \"name\": \"Sample Collection 909\",\n            \"description\": \"This is just a sample collection.\",\n            \"schema\": \"https://schema.getpostman.com/json/collection/v2.1.0/collection.json\"\n        },\n        \"item\": [\n            {\n                \"name\": \"This is a folder\",\n                \"item\": [\n                    {\n                        \"name\": \"Sample POST Request\",\n                        \"request\": {\n                            \"url\": \"https://postman-echo.com/post\",\n                            \"method\": \"POST\",\n                            \"header\": [\n                                {\n                                    \"key\": \"Content-Type\",\n                                    \"value\": \"application/json\"\n                                }\n                            ],\n                            \"body\": {\n                                \"mode\": \"raw\",\n                                \"raw\": \"{\\\"data\\\": \\\"123\\\"}\"\n                            },\n                            \"description\": \"This is a sample POST Request\"\n                        }\n                    }\n                ]\n            },\n            {\n                \"name\": \"Sample GET Request\",\n                \"request\": {\n                    \"url\": \"https://postman-echo/get\",\n                    \"method\": \"GET\",\n                    \"description\": \"This is a sample GET Request\"\n                }\n            }\n        ]\n    }\n}",ParameterType.RequestBody);

var response = RestSharp.Settings.restClient.ExecuteAsync(RestSharp.Settings.restRequest).Result;

Console.WriteLine("Returned http status code is: " + response.StatusCode + " expected OK");

Assert.IsTrue((int)response.StatusCode == 200);

查看 RestSharp快速入门指南 有几种方法可能对您有用。

如果你有一个 object 要序列化,你可以使用AddJsonBody()来发布 Json。这取自该指南:

例如,您只需要这些行来发出主体为 JSON 的请求:

 var request = new RestRequest("address/update").AddJsonBody(updatedAddress); var response = await client.PostAsync<AddressUpdateResponse>(request);

AddJsonBody自动设置ContentTypeDataFormat

使用这些方法时,无需设置Content-Type或将DataFormat参数添加到请求中,RestSharp 会为您完成。


但是,由于您已经将 Json 作为字符串,因此正确使用的方法是AddStringBody() (感谢 Alexey Zimarev 的评论)。 您需要传递内容类型:

如果您有预序列化的有效负载,如 JSON 字符串,则可以使用AddStringBody将其添加为正文参数。 您需要指定内容类型,以便远程端点知道如何处理请求正文。 例如:

 ``` const json = // your json string; request.AddStringBody(json, ContentType.Json); ```

我建议也像上面的示例一样等待响应,而不是使用.Result

尝试这个

var client = new RestClient("http://..");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", json,  ParameterType.RequestBody);
IRestResponse response =  await client.ExecuteAsync(request);
var jsonOutput=response.Content;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM