RestSharp AddBody adding double quote in JSON parameter - c#

I am calling API with RESTSharp
var client = new RestClient("http://demoservice.com");
var request = new RestRequest("callapi", "put");
request.RequestFormat = DataFormat.Json;
string jsonaction = "{\"tokenid\":\"x123x45\",\"userid\":\"2456\",\"ip\":\"192.168.1.20\",\"transaction\":\"6\",\"actionCode\":\"78\",\"jtoken\":\"systemtoken\"}";
request.AddBody(new { action = "SAVE", data = "savedata", token = "systemtoken", jsonaction = jsonaction });
I am checking in debug data passing in request. and my expected output as follows
{"action":"SAVE","data":"savedata","token":"systemtoken","jsonaction":{"tokenid":"x123x45","userid":"2456","ip":"192.168.1.20","transaction":"6","actionCode":"78","jtoken":"systemtoken"}}
But getting
{"action":"SAVE","data":"savedata","token":"systemtoken","jsonaction":"{"tokenid":"x123x45","userid":"2456","ip":"192.168.1.20","transaction":"6","actionCode":"78","jtoken":"systemtoken"}"}
If anybody can guide how to post for JSON I have tried with Addbody and AddJsonBody but nothing works.

You can do this using the request.AddParameter() method:
request.Method = Method.POST;
request.AddHeader("Accept", "application/json");
request.Parameters.Clear();
request.AddParameter("application/json", data , ParameterType.RequestBody);
var response = client.Execute(request);
var content = response.Content; // raw content as string
Where data is of the format:
data :
{
"action":"dosomething" ,
"data":"somedata" ,
"token":"sometoken",
"jsonAction": {
"tokenId": "",
...
}
Hope it helps!

I would suggest you to use JObject to create your body for a request like,
JObject jObject = new JObject();
jObject["action"] = "SAVE";
jObject["data"] = "savedata";
jObject["token"] = "systemtoken";
jObject["jsonaction"] = JObject.Parse("{\"tokenid\":\"x123x45\",\"userid\":\"2456\",\"ip\":\"192.168.1.20\",\"transaction\":\"6\",\"actionCode\":\"78\",\"jtoken\":\"systemtoken\"}");
And then pass this jObject to either
request.AddBody(jObject.ToString());
OR
request.AddJsonBody(jObject.ToString());
And for JObject you need to import using Newtonsoft.Json.Linq; namespace to your program and you can find this namespace in newtonsoft.json package.
You can even use like this
request.AddBody(new { action = "SAVE", data = "savedata", token = "systemtoken", jsonaction = JObject.Parse(jsonaction) });
But creating a JObject for your full json data is best that minimizes the error and exception while creating own json data with string
Output:

From two genius people I could complete following code
jsonaction objjsonaction = new jsonaction();
objjsonaction.tokenid = "x123x45";
objjsonaction.userid = "2456";
objjsonaction.ip = "192.168.1.20";
objjsonaction.transaction = "6";
objjsonaction.actionCode = "78";
objjsonaction.jtoken = gentoken("key"); // gentoken() is external function for generating token from key
string sobjjsonaction = Newtonsoft.Json.JsonConvert.SerializeObject(objjsonaction);
sobjjsonaction = sobjjsonaction.Replace("__", "-");
JObject jObject = new JObject();
jObject["action"] = "SAVE";
jObject["data"] = getpostdata(); // a function to generate data from db
jObject["token"] = gentoken("key"); // gentoken() is external function for generating token from key
jObject["jsonaction"] = JObject.Parse(sobjjsonaction);
string sObject = Regex.Replace(jObject.ToString(), #"\s+", "");
//request.Method = Method.PUT;
request.AddHeader("Accept", "application/json");
request.Parameters.Clear();
request.AddParameter("application/json", sObject, ParameterType.RequestBody);

Related

JSON parsing error when trying to use Face API

I'm trying to send photo from Imgur via URL adress to Microsoft Face API and get ID of face from Json response but when I try to run the code, I always get JSON parsing error. I have no idea what I am doing wrong.
I tried to make this request via Postman and everything is working fine there but in c# it just won't work.
Can you help me please?
static void TryFunction()
{
string host = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true";
string subscriptionKey = "...";
body = new System.Object[] { new { url = #"https://i.imgur.com/... .png" } };
var requestBody = JsonConvert.SerializeObject(body);
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(host);
request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
var response = client.SendAsync(request).Result;
var jsonResponse = response.Content.ReadAsStringAsync().Result;
dynamic json = JsonConvert.DeserializeObject(jsonResponse);
Console.WriteLine(jsonResponse);
}
}
{"error": {"code":"BadArgument", "message":"JSON parsing error."}}
The C# request body looks like this:
[{"url":"https://i.imgur.com/... .png"}]
Whereas the Postman request body looks like this:
{ "url": "https://i.imgur.com/... .png" }
Based on your comment, you are expecting a single object, but generating an array. This is down to the following line of code:
body = new System.Object[] { new { url = #"https://i.imgur.com/... .png" } };
This creates an array with a single item in it. What you actually want is just a single item:
body = new { url = #"https://i.imgur.com/... .png" };
Note that [ ] in JSON is an array, and { } in JSON is an object.

RestSharp post request - Body with x-www-form-urlencoded values

I am using postman and making an api post request where I am adding body with x-www-form-urlencoded key/values and it works fine in postman.
The issue arrises when I try it from c# using RestSharp package.
I have tried the following code below but not getting the response. I get "BadRequest" invalid_client error.
public class ClientConfig {
public string client_id { get; set; } = "value here";
public string grant_type { get; set; } = "value here";
public string client_secret { get; set; } = "value here";
public string scope { get; set; } = "value here";
public string response_type { get; set; } = "value here";
}
public void GetResponse() {
var client = new RestClient("api-url-here");
var req = new RestRequest("endpoint-here",Method.POST);
var config = new ClientConfig();//values to pass in request
req.AddHeader("Content-Type","application/x-www-form-urlencoded");
req.AddParameter("application/x-www-form-urlencoded",config,ParameterType.RequestBody);
var res = client.Execute(req);
return;
}
//Also tried this
req.AddParameter("client_id",config.client_id,"application/x-www-form-urlencoded",ParameterType.RequestBody);
req.AddParameter("grant_type",config.grant_type,"application/x-www-form-urlencoded",ParameterType.RequestBody);
req.AddParameter("client_secret",config.client_secret,"application/x-www-form-urlencoded",ParameterType.RequestBody);
req.AddParameter("scope",config.scope,ParameterType.RequestBody);
req.AddParameter("response_type",config.response_type,"application/x-www-form-urlencoded",ParameterType.RequestBody);
//tried this too
var client = new RestClient("url-here");
var req = new RestRequest("endpointhere",Method.POST);
var config = new ClientConfig();
req.AddBody(config);
var res = client.Execute(req);
this working for me, it was generator from postman
var token = new TokenValidation()
{
app_id = CloudConfigurationManager.GetSetting("appId"),
secret = CloudConfigurationManager.GetSetting("secret"),
grant_type = CloudConfigurationManager.GetSetting("grant_type"),
Username = CloudConfigurationManager.GetSetting("Username"),
Password = CloudConfigurationManager.GetSetting("Password"),
};
var client = new RestClient($"{xxx}{tokenEndPoint}");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", $"app_id={token.app_id}&secret={token.secret}&grant_type={token.grant_type}&Username={token.Username}&Password={token.Password}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
if (response.StatusCode != HttpStatusCode.OK)
{
Console.WriteLine("Access Token cannot obtain, process terminate");
return null;
}
var tokenResponse = JsonConvert.DeserializeObject<TokenValidationResponse>(response.Content);
I personally find this way to work better for me when sending Form-UrlEncoded data.
public void GetResponse() {
var client = new RestClient("api-url-here");
var req = new RestRequest("endpoint-here",Method.POST);
var config = new ClientConfig();//values to pass in request
// Content type is not required when adding parameters this way
// This will also automatically UrlEncode the values
req.AddParameter("client_id",config.client_id, ParameterType.GetOrPost);
req.AddParameter("grant_type",config.grant_type, ParameterType.GetOrPost);
req.AddParameter("client_secret",config.client_secret, ParameterType.GetOrPost);
req.AddParameter("scope",config.scope, ParameterType.GetOrPost);
req.AddParameter("response_type",config.response_type, ParameterType.GetOrPost);
var res = client.Execute(req);
return;
}
Details on this parameter type can be found here:
https://github.com/restsharp/RestSharp/wiki/ParameterTypes-for-RestRequest#getorpost
Personally, I found AddObject() method quite useful, and cleaner when you have so many parameters to add.
public void GetResponse() {
var client = new RestClient("api-url-here");
var req = new RestRequest("endpoint-here",Method.POST);
var config = new ClientConfig();//values to pass in request
req.AddHeader("Content-Type","application/x-www-form-urlencoded");
req.AddObject(config);
var res = client.Execute(req);
return res;
}
If it worked on postman, you can just press the code button on the right hand side. This will provide a working example in multiple languages. It is the button above the information icon. I would post a screenshot of it, but I don't have 10 reputation to do so.
i have found this good for my scenario , it is working perfect,
var client = new RestClient("https://test.salesforce.com/services/oauth2/token");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=password&client_id=3MVG9U_dUptXGpYKew7P.oPwrIsvowP_K4CsnkxHJIEOUJzW0XBUUY3o12bLDasjeIPGVobvBZo8TNFcCY6J3&client_secret=3189542819149073716&username=integraciones%40lamarina.com.mx.dev&password=2Password!4iwZvMQKVAwkYyJRy50JlAHk", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine("Response.StatusCode: " + response.StatusCode);
Console.WriteLine("Response.Content: " + response.Content);
Console.WriteLine("Response.ErrorMessage: " + response.ErrorMessage);
https://dotnetfiddle.net/J64FR5
in my case this is what worked
req.AddParameter("client_id", "unigen-corporation", ParameterType.HttpHeader);
req.AddParameter("grant_type", "client_credentials", ParameterType.GetOrPost);
If you've copied the code from the postman, try removing the following:
request.AlwaysMultipartFormData = true;
In my case after removing this line code worked.
var client1 = new RestClient(URI);
var request1 = new RestRequest(Method.POST);
request1.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request1.AddParameter("client_id", "XX");
request1.AddParameter("client_secret", "XX");
request1.AddParameter("grant_type", "XX");
request1.AddParameter("role", "XX");
IRestResponse response1 = client1.Execute(request1);
System.Console.WriteLine(response1.Content);
Add parameters according to your needs. This work fine!
for my code, this works perfect.
// extention function for object->formed data body string
public static string toFormDataBodyString(this object src)
{
var res = new List<string>();
foreach (var key in src.GetType().GetProperties())
{
res.Add($"{key.Name}={src.GetType().GetProperty(key.Name)?.GetValue(src)}");
}
return string.Join("&", res);
}
//--------------------------------------
var data = new {
param1 = xxxx,
param2 = xxxx
}
var translateReq = new RestRequest("url_here")
.AddHeader("Content-Type", "application/x-www-form-urlencoded")
.AddParameter("application/x-www-form-urlencoded", data.toFormDataBodyString(), ParameterType.RequestBody);

Unable to POST via REST API

This may be too specific of an issue for assistance on, but I'm at a roadblock and don't know where else to turn.
I am POSTing to a website via REST API and their documentation states:
var client = new RestClient("https://server_name/api/import/tickets");
var request = new RestRequest(Method.POST);
request.AddHeader("authorization", "Bearer {{access_token}}");
request.AddHeader("content-type", "application/json");
request.AddHeader("accept", "application/json");
var yourArrayOfTickets = new List<Ticket>();
// TODO: populate the list
request.RequestFormat = DataFormat.Json;
request.AddBody(yourArrayOfTickets);
IRestResponse response = client.Execute(request);
I am sending
public static void MakeTicket(string token, string url,
string clientName, string clientLocation,
string ticketSource, string ticketType,
string title, string priority, string status,
string details, DateTime openDate, string queue)
{
TicketBody ticketBody = new TicketBody();
ticketBody.ClientName = clientName;
ticketBody.ClientLocation = clientLocation;
ticketBody.TicketSource = ticketSource;
ticketBody.TicketType = ticketType;
ticketBody.Title = title;
ticketBody.Priority = priority;
ticketBody.Status = status;
ticketBody.Details = details;
ticketBody.OpenDate = Convert.ToString(openDate.ToString("MM/dd/yyyy HH:mm:ss"));
ticketBody.Queue = queue;
var body = JsonConvert.SerializeObject(ticketBody);
var bodyList = new List<string>();
bodyList.Add(body);
var client = new RestClient(url + "/import/tickets");
var request = new RestRequest(Method.POST);
request.AddHeader("authorization", "Bearer " + token);
request.AddHeader("content-type", "application/json");
request.AddHeader("accept", "application/json");
request.RequestFormat = DataFormat.Json;
request.AddBody(bodyList);
IRestResponse response = client.Execute(request);
}
My bodyList JSON looks like
My response looks like
Their documentation states the required fields are:
The error message is too vague to help me, it just says I'm missing something but doesn't say what, and as far as I can tell, I'm passing in everything it needs.
As per the documentation screenshot, you have not included the required parameter AssgineeUsername. If you are specifying Queue, try passing it as empty, but include it in request.
ticketBody.Queue = queue;
ticketBody.AssgineeUsername = "";
So turns out I was building out the JSON object incorrectly for this. Instead of serializing the entire object, it needs to be a list.
Ticket ticketBody = new Ticket
{
ClientName = clientName,
ClientLocation = clientLocation,
TicketSource = ticketSource,
TicketType = ticketType,
Title = title,
Priority = priority,
Status = status,
Details = details,
OpenDate = Convert.ToString(openDate.ToString("MM/dd/yyyy HH:mm:ss")),
Queue = queue
};
List<Ticket> bodyList = new List<Ticket>();
bodyList.Add(ticketBody);

Can not upload Transient Document to Adobe Sign in C#

I'm trying to upload a transient document file to Adobe Sign in C#, and it has driven me to my wits end trying to get it to work.
I've even contacted Adobe, and even they don't know how to do it.
My code is as follows:
if (!File.Exists(#"documents\1-Registration Form.pdf"))
{
return;
}
Models objGetData = new Models();
RestClient objClient = new RestClient("https://api.na1.echosign.com:443/api/rest/v5");
RestRequest objRequest = new RestRequest("transientDocuments", Method.POST);
objRequest.AddFile("file", File.ReadAllBytes(#"documents\1-Registration Form.pdf"), "1-Registration Form.pdf");
objRequest.AddHeader("Access-Token", "-My Token Here-");
objRequest.RequestFormat = DataFormat.Json;
IRestResponse objResponse = objClient.Execute(objRequest);
var content = objResponse.Content;
JObject jsonLinq = JObject.Parse(content);
try
{
var objResultObjects = AllData(jsonLinq).First(c => c.Type == JTokenType.Array && c.Path.Contains("libraryDocumentList")).Children<JObject>();
}
catch(Exception ex)
{
ex.LogExceptionToDatabase();
}
return;
Here's the response that I'm getting as the result of my most recent attempt:
"{\"code\":\"NOT_FOUND\",\"message\":\"Resource not found\"}"
I typically get Bad request saying the file isn't present or a not found error, but they're not always the same.
All help will be appreciated.
EDIT:
The following code will give a response with a list of library docs so I know it's not the URL.
var objGetData = new Models();
var objClient = new RestClient("https://api.na1.echosign.com:443/api/rest/v5");
var objRequest = new RestRequest("libraryDocuments", Method.GET);
objRequest.AddHeader("Access-Token", "- My Key -");
objRequest.RequestFormat = DataFormat.Json;
objRequest.AddBody(objGetData);
IRestResponse objResponse = objClient.Execute(objRequest);
var content = objResponse.Content;
JObject jsonLinq = JObject.Parse(content);
SOLUTION:
var objClient = new RestClient(#"https://api.na1.echosign.com:443/api/rest/v5/");
var objRequest = new RestRequest(#"transientDocuments", Method.POST);
var thisFile = File.ReadAllBytes( #"documents\1-Registration Form.pdf");
objRequest.AddFile("File", File.ReadAllBytes( #"documents\1-Registration Form.pdf"), "1-Registration Form.pdf");
objRequest.AddHeader("Access-Token", "-MyToken-");
objRequest.RequestFormat = DataFormat.Json;
IRestResponse objResponse = objClient.Execute(objRequest);
var content = objResponse.Content;
JObject jsonLinq = JObject.Parse(content);
This did it. Apparently "file" is bad but "File" is okay.
var objClient = new RestClient(#"https://api.na1.echosign.com:443/api/rest/v5/");
var objRequest = new RestRequest(#"transientDocuments", Method.POST);
var thisFile = File.ReadAllBytes( #"documents\1-Registration Form.pdf");
objRequest.AddFile("File", File.ReadAllBytes( #"documents\1-Registration Form.pdf"), "1-Registration Form.pdf");
objRequest.AddHeader("Access-Token", "-MyToken-");
objRequest.RequestFormat = DataFormat.Json;
IRestResponse objResponse = objClient.Execute(objRequest);
var content = objResponse.Content;
JObject jsonLinq = JObject.Parse(content);
Not sure if the URI is correct, check missing '/' on the end of the RestClient (shouldn't be needed but still).
Lastly if you browse to the location in Angular 1 does it give you the same issue? I ask as this is a lot lighter to test and you can see using F12 developer tools exactly what is coming back from the open https channel.

Get JSON response using RestSharp

I'm new to C# and I'm trying to get the JSON response from a REST request using RestSharp;
The request I want to execute is the following one : "http://myurl.com/api/getCatalog?token=saga001". It works great if I'm executing it in a browser.
I've tried this :
var client = new RestClient("http://myurl.com/api/");
var request = new RestRequest("getCatalog?token=saga001");
var queryResult = client.Execute(request);
Console.WriteLine(queryResult);
And I get "RestSharp.RestReponse" instead of the JSON result I'm hopping for.
Thanks for your help !
Try:
var client = new RestClient("http://myurl.com/api/");
var request = new RestRequest("getCatalog?token={token}", Method.GET);
request.AddParameter("token", "saga001", ParameterType.UrlSegment);
// request.AddUrlSegment("token", "saga001");
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
var queryResult = client.Execute(request);
Console.WriteLine(queryResult.Content);
This is old but I was just struggling with this too. This is the easiest way I found.
var client = new RestClient("http://myurl.com/api/");
var request = new RestRequest("getCatalog?token=saga001");
var response = client.Execute(request);
if (response.StatusCode == HttpStatusCode.OK)
{
// Two ways to get the result:
string rawResponse = response.Content;
MyClass myClass = new JsonDeserializer().Deserialize<MyClass>(response);
}
Try as below:
var client = new RestClient("http://myurl.com/api/");
client.ClearHandlers();
var jsonDeserializer = new JsonDeserializer();
client.AddHandler("application/json", jsonDeserializer);
var request = new RestRequest("getCatalog?token=saga001");
var queryResult = client.Execute(request);
Console.WriteLine(queryResult);
If you want to save the result into JSON file:
You should use these namespaces:
using RestSharp;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
var client = new RestClient("http://myurl.com/api/");
var request = new RestRequest(Method.GET);
request.AddHeader("content-type", "application/json");
var queryResult = client.Execute<Object>(request).Data;
string json = JsonConvert.SerializeObject(queryResult);
System.IO.File.WriteAllText(#"C:\...\path.json", json);

Categories

Resources