我遇到一个特殊API的非常奇怪的行为。我在Postman和cUrl中发布了一些数据并获得了正常响应,但是使用RestSharp获得了422响应。
这是我试图发送的JSON:
{
"sale": [
{
"external_id": "",
"sold_at": "01/15/2018",
"sold_to": "anonymous",
"discount_total": "0.00",
"price_total": "0.00",
"tax_total": "0.00",
"reason": "",
"status": "sale",
"cog_total": "0.00",
"global_sold_by_user_id": "WASTATE1.US2I9",
"global_area_id": "WAGAG2.ARASF",
"global_customer_id": "",
"sale_items": [
{
"external_id": "",
"use_by_date": "01/15/2018",
"description": "HOC-Do Si Do 1G Package",
"type": "marijuana",
"sold_at": "01/15/2018",
"qty": "1.000000",
"uom": "gm",
"unit_price": "55.000000",
"discount_total": "0.00",
"price_total": "0.00",
"tax_total": "0.00",
"potency": "",
"returned_reason": "",
"returned_at": "",
"total_marijuana_in_grams": "1.000000000000",
"name": "",
"unit_cog": "0.00",
"global_customer_id": "",
"global_batch_id": "WAGAG2.BA32H",
"global_returned_by_user_id": "",
"global_inventory_id": "WAG2.IN171",
"sale_item_taxes": []
}
]
}
]
}
这是我的代码段:
var client = new RestClient("https://watest.leafdatazone.com");
var request = new RestRequest(args.Path, args.IsPost ? Method.POST : Method.GET);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Content-Type", "application/json");
request.AddHeader("x-mjf-mme-code", "<my_code>");
request.AddHeader("x-mjf-key", "<my_key>");
if (args.IsPost)
{
request.AddParameter("application/json; charset=utf-8", args.RequestJSON, ParameterType.RequestBody);
}
var response = client.Execute(request);
任何想法可能会发生什么?我也尝试过HttpClient,结果相同。具有相同精确标题的完全相同的JSON在Postman中工作正常。我甚至在我的代码和示例中使用了Postman的代码片段,仍然使用422。