-2

I am testing a service to which you send a json and it returns a PDF with the data you sent it. I did tests with Insomnia as shown here:

test with insomnia

It works correctly, but trying to recover the file through code always generates an error. My code is the following:

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

This connects correctly with the API but apparently the json has not been sent correctly. Any idea why this happens?

I created the json with dynamic and tried to send it.

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

But this does not work either.

When I print response.Content in console it shows me the following: "Message": "Internal error when trying to generate the PDF" Value can not be null. \ R \ nParameter name: source "," StatusCode ": 3," Result ": null, "ResultList": null}

mkl
  • 90,588
  • 15
  • 125
  • 265

1 Answers1

1

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

It seems like you are trying to add a parameter with the name "application/json". You may need to specify the name of the parameter in the endpoint and use AddObject instead of AddParameter. We can help more if you can give us some more info on your endpoint.

Marius
  • 1,420
  • 1
  • 11
  • 19
  • The endpoint was provided to me only to consume it. I know it works because I did tests with Insomnia. The Endpoint receives the json that I send and returns a PDF with the information of that json in bytes. Here is an example of what it receives and what it returns. [link] (https://i.stack.imgur.com/k9pJZ.png) – Angel Martinez Nov 23 '18 at 20:46