Get Pdf Rest Sharp (GET) - c#

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:
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}

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.

Related

Restsharp Request gives "Argument 1: cannot convert from 'RestSharp.Method' to 'string?'" error

I am trying to call POST API request using restSharp 108.0.1v.
The code is as below;
var client = new RestClient("https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles");
var request = new RestRequest(Method.Post);
request.AddHeader("x-api-key", "MYAPIKEY");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n\t\"registrationNumber\":\"AA19AAA\"\n}", ParameterType.RequestBody);
RestResponse response = client.Execute(request);
The snippet Method.Post of var request = new RestRequest(Method.Post);gives the error that I mentioned.
Please help me to solve this issue ?
Looking at the documentation here the first parameter of the RestRequest constructor is the subpath to the resource you want to access. Instead you should do something like the following
var client = new RestClient("https://driver-vehicle-licensing.api.gov.uk");
var request = new RestRequest("vehicle-enquiry/v1/vehicles", Method.Post);
// ... or I believe this should work as well:
var client = new RestClient("https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1");
var request = new RestRequest("vehicles", Method.Post);

request webapi with restharp

Goodmorning everyone,
using postan I make a request as in the figure, and I get data.
I write using the same call (at least I assume) and it returns an empty string and not a json.
Where could it be the mistake?
Thank you all
var client = new RestClient("http://pp.miosito.it/API/");
var request = new RestRequest("STHQRY", Method.POST);
// request.RequestFormat = DataFormat.Json;
request.AddParameter("TblName", "STSIG$");
var response = client.Execute(request);
var content = response.Content;
enter image description here
Query Parameter Detail is mentioned in add Parameter. Please change the code as below
Code:
var client = new RestClient("http://pp.miosito.it/API/");
var request = new RestRequest("STHQRY?TblName=STSIG$", Method.POST);
//Request Body detail needs to be added.
//I assumed input is JSON. Please change the header part , if the body has different
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", body, ParameterType.RequestBody);
var response = client.Execute(request);
var content = response.Content;

my webservice restful client fail post c# bearer

I Write one application to access one Webservice Restful using C#, i get the Token with success, i access other services without parameters fine, but when i need to send parameters in POST method it´s not work.
In other side, the Wesbservice dont see my post.
Can someone help-me about this ?
Here is my code.
string urlMethod = metodo;
// "/api/v1/organizacao/criar";
var accessToken = IntegraPb.GetToken();
var client = new RestClient(Sincronizador.Properties.Settings.Default.apiUrl);
var request = new RestRequest(urlMethod, Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddHeader("Authorization", "Bearer " + accessToken);
var jsserie = new System.Web.Script.Serialization.JavaScriptSerializer();
// obj is my class to serialize
request.AddJsonBody(jsserie.Serialize(obj));
IRestResponse response = client.Execute(request);
This image is the request object
AddJsonBody receives a object, not a serialized string of your object. It does the serialization internally.
So use this instead:
request.AddJsonBody(obj);

Creating a basecamp TODO returns a 403 forbidden (restsharp, asp.net mvc, basecamp api)

I can get a todo without any problem, but when I POST a todo in basecamp I get 403 forbidden
Here is my code
CODE
var client = new RestClient()
{
BaseUrl = "https://basecamp.com/" + accountId + "/api/v1/projects/" + projectId + "/todolists/" + todolistId + "/todos.json"
};
string baseCampClientId = WebConfigurationManager.AppSettings["bcClientId"];
string baseCampClientSecret = WebConfigurationManager.AppSettings["bcClientSecret"];
var request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
string json = JsonConvert.SerializeObject(todo);
request.AddHeader("User-Agent", "MyApp (my#email.com)");
request.AddHeader("Content-type", "application/json");
request.AddHeader("charset", "utf - 8");
request.AddHeader("Authorization", userInfo.BaseCampToken);
request.AddParameter("client_id", baseCampClientId);
request.AddParameter("client_secret", baseCampClientSecret);
request.AddParameter("access_token", userInfo.BaseCampToken);
request.AddBody(json);
var result = client.Execute(request);
string jsonResult = result.Content;
The response is always 403 Forbidden
The json which I send in the request body is
JSON
"{\"content\":\"solve this thing\",\"due_at\":\"2014-07-10T00:00:00.0000000\",\"assignee\":{\"id\":\"1111111\",\"type\":\"Person\"}}"
and here is the image of the request
Please help me to fix this 403 forbidden issue
well i tried your code with only changing one thing and it worked for me. my changes were:
client.Authenticator = new Auth2AuthorizationRequestHeaderAuthenticator(userInfo.BaseCampToken) for
request.AddParameter("access_token", userInfo.BaseCampToken);
also try removing "assignee" from your json data if you have any.

GoToWebinar - Request not in expected format - RestSharp

Similar to this forum post:
https://developer.citrixonline.com/forum/request-not-expected-format
However he didn't really explain what he found was wrong with his code.
I'm using RestSharp to make API calls. I've been able to get it to work great to pull an list of upcoming webinars however when I try to register participant I keep getting a 400/Request not in expected format error.
Following this documentation https://developer.citrixonline.com/api/gotowebinar-rest-api/apimethod/create-registrant, here is my code:
var client = new RestClient(string.Format("https://api.citrixonline.com/G2W/rest/organizers/{0}/webinars/{1}/registrants", "300000000000xxxxxx", btn.CommandArgument));
var request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddHeader("Accept", "application/json");
request.AddHeader("Accept", "application/vnd.citrix.g2wapi-v1.1+json");
request.AddHeader("Authorization", "OAuth oauth_token=" + System.Configuration.ConfigurationManager.AppSettings["GoToWebinar"]);
request.AddParameter("firstName", LoggedInUser.FirstName);
request.AddParameter("lastName", LoggedInUser.LastName);
request.AddParameter("email", LoggedInUser.Email);
var response = client.Execute(request);
var statusCode = response.StatusCode;
Any insights on how I can figure out why I keep getting that error?
Thank you!
Instead of using AddParamter (which adds key/value parameters to the request body), you need to write JSON instead:
request.DataFormat = DataFormat.Json;
request.AddBody(new {
firstName = LoggedInUser.FirstName,
lastName = LoggedInUser.LastName,
email = LoggedInUser.Email
});
You'll also need to clear the handlers (which automatically set the Accept header) if you want to specify them directly:
client.ClearHandlers();

Categories