【问题标题】:How to use Restsharp to correctly add Request Payload?如何使用 Restsharp 正确添加 Request Payload?
【发布时间】:2019-05-01 13:59:22
【问题描述】:

我正在尝试使用 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 body 返回预期结果,但在 c# 中没有。帮助表示赞赏。

【问题讨论】:

  • 你能用 Fiddler(或类似的东西)来比较 Postman 和你的应用发出的请求,看看有什么不同吗?

标签: c# restsharp


【解决方案1】:

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

由于以下原因导致请求错误(400)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-04
    • 2011-07-03
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多