简体   繁体   中英

How to use Restsharp to correctly add Request Payload?

I'm trying to use RestSharp to use query rest, but I always get Bad Request response. The code:

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);

The request type is POST and with Request Payload in JSON (as shown in Chrome) the value as follows:

{"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}

I tried in Postman with Raw body returns the expected result but not in c#. Helps appreciated.

Please check your Json format.Payload json might be incorrect format.

Got bad request(400) due to following reason

1.Your browser sent a request that this server could not understand. 2.Invalid URL 3.The request could not be understood by the server due to malformed syntax. The client should not repeat the request without modifications 4.The request hostname is invalid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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