[英]Restsharp request parameter syntax issue
這是請求數據的示例
{
"delivery_needed": "false",
"merchant_id": "201",
"merchant_order_id": "123456",
"amount_cents": "25000",
"currency": "USD",
"items": [],
"shipping_data": {
"name": "test_user",
"street": "sample street",
"city": "cairo",
}
}
我編寫了C#代碼,但似乎運輸數據和項目的語法有誤。
這是我得到的錯誤
{\\“ shipping_data \\”:{\\“ non_field_errors \\”:[\\“無效的數據。需要一個字典,但是得到了str。\\”]},\\“ items \\”:[\\“預計了項目列表但得到了類型\\\\ “STR \\\\” \\“]}
這是我編寫的C#代碼,我對語法感到困惑
var client = new RestClient("url");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", request.AddJsonBody(new { delivery_needed = "false", merchant_id = "201", merchant_order_id = "123456", amount_cents = "25000", currency = "USD", items = "[]", shipping_data = "{ ", name = "test_user", street = "sample street", city = "Cairo"}), ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
當前,您使用格式錯誤的數據構造了請求的主體。
當前,您具有...shipping_data = "{ ",...
,這只是一個字符串,如期望適當的對象模型或鍵/值對(字典)時所指示的錯誤。
items
根據該錯誤消息還沒有正確設置。 它期望一個數組,但是它又提供了一個字符串"[]"
您需要正確構建模型
var model = new {
delivery_needed = "false",
merchant_id = "201",
merchant_order_id = "123456",
amount_cents = "25000",
currency = "USD",
items = new object[0], //<--This needs to be an array
shipping_data = new { //<--This needs to be a proper object
name = "test_user",
street = "sample street",
city = "Cairo"
}
};
var client = new RestClient("url");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddJsonBody(model); //<-- this will serialize and add the model as a JSON body.
IRestResponse response = client.Execute(request);
注意將要解析為請求主體的模型的構造。 看看它與OP中的示例有多相似
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.