簡體   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