Below code I need to call in c#, how do we can achieve this, please help me.
import requests #requires "requests" package
import json
response = requests.post('https://scm.commerceinterface.com/api/v3/mark_exported', data={'supplier_id':'111111111', 'token':'sample-token',
'ci_lineitem_ids':json.dumps([54553919,4553920])}).json()
if response['success'] == True:
#Successfully marked as exported (only items which are not already marked exported)
pass
else:
pass
//I got sollution
C# post request
var client = new RestClient(exportUrl);
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/json");
request.AddParameter("supplier_id", apiSupplierID);
request.AddParameter("token", apiToken);
request.AddParameter("ci_lineitem_ids", exportOrders);
IRestResponse response = client.Execute(request);
Related
I am developing an small app with C# window form.
I can use Restsharp to send any request with content type application/json. But with application/x-www-form-urlencoded the server always return nothing or Internal error.
I have tested this api with Postman and Restlet Client and it work well.
Following some example on internet but it not work too.
Here is my code:
var client = new RestClient("http://www.nhimanhma.com");
var request = new RestRequest("users/sign_in", Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
string formData = $"token={token}&user[email]={email}&user[password]={password}";
string urlEncode = RestSharp.Extensions.StringExtensions.UrlEncode(formData);
request.AddParameter("application/x-www-form-urlencoded", urlEncode, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var content = response.Content;
I am trying to send Header and Request in post method using RestClient library but getting error:
Endpoint not found.
var client = new RestClient("http://xxxxxxxxxx/UIService.svc/xxxxxxxxx/xxxxx");
var request = new RestRequest();
request.Method = Method.POST;
request.AddHeader("Authentication", jsonHeadEncrpt);
// request.AddHeader("Accept", "application/json");
request.Parameters.Clear();
request.AddParameter("application/json", JsonReqEncrpt, ParameterType.RequestBody);
var responsed = client.Execute(request);
Response :Endpoint not found. Please see the service help page for constructing valid requests to the service
Guys Found the solution need to add request.Resource where need to assign the endpoint function..Now it is working fine
I have a C# REST Web API and I have some code like this that makes a request to an endpoint. Some of the data that I want to pass along is an object of my own type and since it is a complex object, I would like to pass it using POST.
RestClient client = new RestClient(Constants.Endpoints.serviceEndPoint)
{
Timeout = 1000
};
string requestResource = Constants.Endpoints.apiEndPoint;
RestRequest request = new RestRequest(requestResource, Method.POST);
request.AddParameter("Authorization", $"Bearer {accessToken}", ParameterType.HttpHeader);
request.AddHeader("Accept", "application/json");
request.AddParameter("id", id, ParameterType.UrlSegment);
request.AddParameter("text/json", objectIWantToSerialize, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
On the other side, I am trying to read the object itself with some code like this
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var content in provider.Contents)
{
// what should I do here to read the content as a JSON
// and then transform it as the object it used to be before the call?
}
I tried to do await content.ReadAsJsonAsync< MyType>(); but also tried await content.ReadAsStringAsync(); and none of these worked. Am I doing something wrong at the time I execute in the client? Or is it something that I am doing on the other side while reading the content?
Instead of this line:
request.AddParameter("text/json", objectIWantToSerialize, ParameterType.RequestBody);
You should use the .AddBody(object) method.
So you're code would look like this:
RestRequest request = new RestRequest(requestResource, Method.POST);
//add other headers as needed
request.RequestFormat = DataFormat.Json;
request.AddBody(objectIWantToSerialize);
IRestResponse response = client.Execute(request);
On the server, if you're using MVC/WebAPI, you can just put the C# type as the input and ASP.NET will deserialize it for you. If not, can you provide more context about how you're receiving the request?
I want to create/modify an issue on redmine using the PUT/POST methods of restSharp.
I cannot find valuable information about xml PUT/POST using Rest sharp. I tried various methods from restsharp.org like Addbody("test", "subject"); , IRestResponse response = client.Execute(request); but there is no change in Redmine. What am I doing wrong?
POST gives a "Only get, put, and delete requests are allowed." message.
PUT gives a "Only get, post, and delete requests are allowed." message.
My Code
RestClient client = new RestClient(_baseUrl);
client.Authenticator = new HttpBasicAuthenticator(_user, _password);
RestRequest request = new RestRequest("issues/{id}.xml", Method.POST);
request.AddParameter("subject", "Testint POST");
request.AddUrlSegment("id", "5");
var response = client.Execute(request);
The problem was in the serialization. My Issue class contains object of various other classes which was causing a problem in the serialization.
This is how we did it:
RestRequest request = new RestRequest("issues/{id}.xml", Method.PUT);
request.AddParameter("id", ticket.id, ParameterType.UrlSegment);
request.XmlSerializer = new RedmineXmlSerializer();
request.AddBody(ticket);
RestClient client = new RestClient(_baseUrl);
client.Authenticator = new HttpBasicAuthenticator(_user, _password);
IRestResponse response = client.Execute(request);
Your code looks ok to me, I'm unsure if you need this but we added this header when using RestSharp for json against a WebAPI host:
request.AddHeader("Accept", "application/xml");
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();