如何使用Restsharp正确添加请求有效载荷?

时间:2019-05-01 13:59:23

标签: c# restsharp

我试图使用RestSharp来使用查询休息,但是我总是收到错误的请求响应。代码:

var url = "https://gql.tokopedia.com/graphql/";
var request = new RestRequest(Method.POST);
var client = new RestClient(url);

request.RequestFormat = DataFormat.Json;
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Content-Type", "application/json");
request.AddBody(payLoad);
IRestResponse response = client.Execute(request);

请求类型为POST,并且在JSON中具有“请求有效负载”(如Chrome中所示),其值如下:

{"variables":{"sid":"987569","page":1,"perPage":80,"etalaseId":"etalase","sort":1},"query":"query ShopProducts($sid: String!, $page: Int, $perPage: Int, $keyword: String, $etalaseId: String,  $sort: Int){\n  GetShopProduct(shopID: $sid, filter: { page: $page, perPage: $perPage, fkeyword: $keyword, fmenu: $etalaseId, sort: $sort }){\n    status\n    errors\n    links {\n      prev\n      next\n    }\n    data {\n      name\n      product_url\n      product_id\n      price {\n        text_idr\n      }\n      primary_image{\n        original\n        thumbnail\n        resize300\n      }\n      flags{\n        isSold\n        isPreorder\n        isWholesale\n        isWishlist\n      }\n      campaign {\n        discounted_percentage\n        original_price_fmt\n        start_date\n        end_date\n      }\n      label{\n        color_hex\n        content\n      }\n      badge{\n        title\n        image_url\n      }\n      stats{\n        reviewCount\n        rating\n      }\n      category{\n        id\n      }\n    }\n  }\n}","operationName":null}

我在Postman中尝试使用Raw主体返回预期结果,但在c#中却没有。帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

请检查您的Json格式。Payloadjson格式可能不正确。

由于以下原因收到了错误的请求(400)

1。您的浏览器发送了此服务器无法理解的请求。 2.无效的网址 3.由于语法错误,服务器无法理解该请求。客户不得在未经修改的情况下重复请求 4.请求的主机名无效。