获取Pdf Rest Sharp(GET)

时间:2018-11-23 18:57:00

标签: c# json rest pdf sharp

我正在测试向您发送json的服务,它会返回包含您发送的数据的PDF。我对失眠症做了测试,如下所示:

test with insomnia

它可以正常工作,但是尝试通过代码恢复文件始终会产生错误。我的代码如下:

var client = new RestClient("Api");
var request = new RestRequest(Method.GET);

request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\n\t\"QuotationId\": 0,\n\t\"QuotationName\": \"CasaLomas\",\n\t\"UserGUID\": \"75DB06AC-E349-4C47-A5AF-87EC63A9B8A9\",\n\t\"QuotedQuantity\": 5,\n\t\"TotalPrice\": 500,\n\t\"CopyEmail\": \"angelmg50@hotmail.com\",\n\t\"MaterialDetails\": [\n\t\t{\n\t\t\t\"SKU\": \"C05423082015140001\",\n\t\t\t\"Name\": \"PORC KONE WHITE MATE 60x30x1\",\n\t\t\t\"Quantity\": 1,\n\t\t\t\"Price\": 638.09,\n\t\t\t\"Amount\": 6380\n\t\t},\n\t\t{\n\t\t\t\"SKU\": \"C05423082015140001\",\n\t\t\t\"Name\": \"PORC KONE WHITE MATE 60x30x1\",\n\t\t\t\"Quantity\": 1,\n\t\t\t\"Price\": 638.09,\n\t\t\t\"Amount\": 6380\n\t\t},\n\t\t{\n\t\t\t\"SKU\": \"C05423082015140001\",\n\t\t\t\"Name\": \"PORC KONE WHITE MATE 60x30x1\",\n\t\t\t\"Quantity\": 1,\n\t\t\t\"Price\": 638.09,\n\t\t\t\"Amount\": 6380\n\t\t}\n\t]\n}", ParameterType.RequestBody);

IRestResponse response = client.Execute(request);

这与API正确连接,但显然json尚未正确发送。知道为什么会这样吗?

我用dynamic创建了json并尝试发送它。

dynamic cotizacion = new JObject();

cotizacion.QuotationId = 0;
cotizacion.QuotationName = "Casa lomas 3";
cotizacion.UserGUID = "75DB06AC-E349-4C47-A5AF-87EC63A9B8A9";
cotizacion.QuotedQuantity = 6;
cotizacion.TotalPrice = 456.12;
cotizacion.CopyEmail = "angelmg50@hotmail.com";
cotizacion.MaterialDetails = new JArray(new JObject(
  new JProperty("SKU", "C05423082015140001"),
  new JProperty("Name", "PORC KONE WHITE MATE 60x30x1"),
  new JProperty("Quantity", 1),
  new JProperty("Price", 638.09),
  new JProperty("Amount", 6380.30)));

var client = new RestClient("Api");
var request = new RestRequest(Method.GET);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", cotizacion, ParameterType.RequestBody);

IRestResponse response = client.Execute(request);

但这也不起作用。

当我在控制台中打印response.Content时,它向我显示以下内容:“消息”:“尝试生成PDF时发生内部错误”值不能为null。 \ R \ n参数名称:来源“,” StatusCode”:3,“结果”:null,“ ResultList”:null}

1 个答案:

答案 0 :(得分:1)

https://github.com/restsharp/RestSharp/wiki/Recommended-Usage

似乎您正在尝试添加名称为"application/json"的参数。您可能需要在端点中指定参数的名称,并使用AddObject代替AddParameter。如果您可以向我们提供有关端点的更多信息,我们可以提供更多帮助。