I code C#. In my C# code add parameter "aliasID" and send request without "aliasID".
But when I use Postman, I add "aliasID" and it was sent.
Code
namespace TrainEyeQTech.ViewModel
{
internal static class SetupAPI
{
public static void Register()
{
RestClient client = new RestClient("faceid/api/domain/register");
//client.Timeout = -1;
var request = new RestRequest("", Method.Post);
request.AddHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiaW50ZXJuYWwtcmVnaXN0ZXIiLCJzZXJ2aWNlIjoiZmFjZWlkLXJlZ2lzdGVyIiwiZXh0cmEiOiJpbnRlcm5hbC1kZXYiLCJpYXQiOjE2MzUwODgzMjAsImV4cCI6MTY2NjYyNDMyMH0.8CBqXQNuCYXBjK7Nt4_fw1rZPIgK6WdZRbhIvWyaS1s");
request.AddHeader("Content-Type", "multipart/form-data; boundary=<calculated when request is sent>");
request.AddHeader("Cookie", "__cflb=02DiuDSnUsPgiMQzVNimjJdcPn2DXK4ZqAx6U3Qmmk1QQ");
request.AddHeader("Content-Length", "<calculated when request is sent>");
request.AddHeader("Connection", "keep-alive");
//request.AddHeader("Host", "<calculated when request is sent>"); here will be error
request.AddHeader("User-Agent", "PostmanRuntime/7.29.2");
request.AddHeader("Accept-Encoding", "gzip, deflate, br");
request.AddHeader("Postman-Token", "<calculated when request is sent>");
request.AddParameter("organization", "internal-dev");
request.AddFile("images[]", "D:\\CLOUDX\\EyeQTech\\faceid\\itsme.jpg");
request.AddParameter("aliasId", "123_cuong"); // here don't add aliasId prameter
RestResponse response = client.Execute(request);
Console.WriteLine(response.Content); //{messenge: missing aliasID}
}
}
Postman
enter image description here
Here is headers of Postman
enter image description here
Related
I have weird question, I can't add both parameter specially aliasId and file Image in same Debug configuration
If I remove line request.AddHeader("Content-Type", "multipart/form-data; boundary=<calculated when request is sent>"); if I keep it I can't add the Parameter, other way if I remove it I can add Image but wrong type.
public static string Register(/*GuestData guestData*/)
{
string urlImage = "D:\\CLOUDX\\EyeQTech\\faceid\\itsme.jpg";
RestClient client = new RestClient("api/domain/faceid/");
var request = new RestRequest("register", Method.Post);
request.AddHeader("Authorization", token);
request.AddHeader("Cookie", "__cflb=02DiuDSnUsPgiMQzVNimjJdcPn2DXK4ZqAx6U3Qmmk1QQ");
request.AddHeader("Content-Type", "multipart/form-data; boundary=<calculated when request is sent>"); //add content type is here
request.RequestFormat = DataFormat.Json;
request.AddFile(name: "images[]", path: urlImage); // add file to body
request.AddBody(new Dictionary<string, string>()
{{"organization", "internal-dev"},
{"aliasId", "900"}
}); //add body parameter
RestResponse response = client.Execute(request);
MessageBox.Show(response.Content);
Console.WriteLine(response.Content);
return response.Content;
}
I would like to know how to convert this Twilio CURL code to RestClient
I am stuck in the request.AddParamenter() I have not idea how to format it order to pass the Twilio SID,Token, From, To and Body Text Message.
curl -X POST https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json \
-data-urlencode "Body=Hi there" \
-data-urlencode "From=+15017122661" \
-data-urlencode "To=+15558675310" \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
To this:
var client = new RestClient("https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("cache-control", "no-cache");
request.AddParameter("application/x-www-form-urlencoded", "bodykey=bodyval", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
This is my code now that compile 100% and after I run the code I get a response "Complete" no error message and no entry at twilio dashboard either, it does not send the text message, any idea your help will be much appreciate.
RestClient client = new RestClient("https://api.twilio.com/2010-04-01/Accounts/ACet53f18a4734c339488c1845e619dd9g/Messages.json");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("ACet53f18a4734c339488c1845e619dd9g:daskshdsjkahkashd90ud09as8dasjkhdsa9"));
request.AddHeader("Authorization", "Basic " + base64authorization);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("Body", "Hello World");
request.AddParameter("From", "+15017122661"); //- My Twilio number goes
request.AddParameter("To", "+15017122661");
IRestResponse response = client.Execute(request);
string ResStatus = response.ResponseStatus.ToString(); //- Complete after successfully run.
You can send a RestClient request like this.
RestClient client = new RestClient("https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN"));
request.AddHeader("Authorization", $"Basic {base64authorization}");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("Body", "Hi");
request.AddParameter("From", "+15017122661");
request.AddParameter("To", "+ 15558675310");
IRestResponse response = client.Execute(request);
I've been trying to get the DHL interface up and running for some time now.
Unfortunately, there is already a lack of authentication.
Request:
client_id == AppID
client_secret = AppToken
var client = new RestClient("https://api.dhlecs.com/auth/v4/accesstoken");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("grant_type", "client_credentials");
request.AddParameter("client_id", client_id);
request.AddParameter("client_secret", client_secret);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
Response:
{"type":"https://api.dhlecs.com/docs/errors/401.0000007", "title":"Invalid
credentials"}
Does anyone have any idea why it doesn't work? Or am I calling something wrong?
I was search this article Consuming Drupal RestApi with c#
And http://tylerfrankenstein.com/code/drupal-services-csrf-token-firefox-poster
I has question in the cookie and token. I have test in poster with firefox and successful on post the created article .Tamper Data has the request header.
tamper data
nid: "129342"
uri: http://www.tsghy.com.cn/services/node/129342
Postman created the post code
var client = new RestClient("http://www.tsghy.com.cn/services/node");
var request = new RestRequest(Method.POST);
request.AddHeader("postman-token", "5c28c9d6-d640-a4f0-a549-b6018e62907d");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("x-csrf-token", "s0Z17LT7neX_K6grHgoJCUPR6VcL2QxRlNLmbRWeExE");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "type=article&title=test%201", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
I will crazy,
And replace to the C# ,i has Access denied for user anonymous.this my code with below :
First I login the Drupal with Rest
private login_user2 loginAsync2(string username, string password)
{
try
{
RestClient client = new RestClient(base_url2);
var request = new RestRequest("user/login.json", Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
client.Authenticator = new SimpleAuthenticator("username",username,"password",password);
var restResponse = client.Execute(request);
var content = restResponse.Content;
if (restResponse.StatusCode==System.Net.HttpStatusCode.OK)
{
login_user2 loginuser = JsonConvert.DeserializeObject<login_user2>(content.ToString());
request = new RestRequest("session/token", Method.GET);
restResponse = client.Execute(request);
loginuser.session_token = restResponse.Content.ToString();
return loginuser;
}
else {
return null;
}
}
catch (Exception ex) { throw ex; }
}
I have question at the login/user->token and session/token ,which the difference?
Second,Post the create data:
RestClient client = new RestClient(base_url2);
var request = new RestRequest("node", Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/json; charset=UTF-8");
request.AddHeader("Accept", "application/json");
request.AddHeader("cookie", "Drupal.toolbar.collapsed=0; "+current_user2.session_name+"="+current_user2.sessid+"; has_js=1");
request.AddHeader("x-csrf-token",current_user2.session_token);
request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0");
request.AddParameter("application/json", myjobject, ParameterType.RequestBody);
var queryresult = client.Execute(request);
yes,I found the key,restful has bug at the request.addheader(cookie,cookie);
change to :request.AddParameter(session_name,session_id,parametertype.cookie);
I am trying to post a new customer to a web api. When I execute the following in Fiddler I get no errors:
POST https://api.someurl.com/api/businesses/99999999/contacts HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json
Accept: application/json
Host: api.someurl.com
Authorization: TOKEN json:{"authenticationCode":"kLJBlaUCK0pYY6YXI6qB3CHamq0=","expiryDate":1468083160641,"locale":"en_US","myUserId":88888888888,"restriction":null,"site":"api.someurl.com"}
Content-Length: 931
{"business":9999999999,"collaborationContext":0,"contactInformation":{"campaign":null,"city":"SomeCity","country":"SomeCountry","email":"email#someurl.com","fax":null,"first":"FName","hasShippingAddress":false,"instructions":null,"last":"LName","locale":null,"mobile":null,"name":"FullName","phone":"222.333.4455","postalOrZipCode":"90210","provinceOrState":null,"readOnly":false,"referral":null,"registration":null,"shippingCity":null,"shippingCountry":null,"shippingName":null,"shippingNotes":null,"shippingPhone":null,"shippingPostalOrZipCode":null,"shippingProvinceOrState":null,"shippingStreet1":null,"shippingStreet2":null,"street1":null,"street2":null,"tollFree":null,"url":null},"currency":null,"defaultTaxCode":null,"id":0,"incomeOrExpenseAccount":null,"linkedBusiness":null,"payableAccount":null,"paymentAccount":null,"paymentTerms":null,"prefix":null,"receivableAccount":null,"removed":false,"type":"CUSTOMER"}
When I try to execute (what I think is) the equivalent (see below), I get "StatusCode: UnsupportedMediaType, Content-Type: text/xml, Content-Length: 0)"
private static string PostContact(string busId, Contact contact )
{
var restClient = new RestClient("https://api.someurl.com");
var jsonToken = GetJsonToken();
// var contactJson = JsonConvert.SerializeObject(contact);
var req = new RestRequest("/api/businesses/{business-id}/contacts");
req.Method = Method.POST;
req.Parameters.Clear();
req.AddHeader("Content-Type", "application/json");
req.AddHeader("Accept", "application/json");
req.AddUrlSegment("business-id", busId);
req.AddParameter("Authorization", string.Format("TOKEN json:{0}", jsonToken), ParameterType.HttpHeader);
req.AddParameter("application/json", contact, ParameterType.RequestBody);
var resp = restClient.Execute(req);
Console.WriteLine(resp.StatusCode);
return resp.Content;
}
Is there a special order to adding parameters or headers, or am I missing something?
Resolved!
var client = new RestClient(url);
var request = new RestRequest(Method.POST);
request.AddHeader("Cache-Control", "no-cache");
///request.AddHeader("Authorization", "Bearer " + access_token);
request.AddHeader("Authorization", token);
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("undefined", JsonRequest, ParameterType.RequestBody);
var response = client.Execute(request);
var responseJson = response.Content;
I have another easier solution:
Add this single line (if NO file is attached)
req.AlwaysMultipartFormData = true;
In my case a piece of source code look like this:
var client2 = new RestClient("https://myurl.com/api/site/Create");
client2.Options.MaxTimeout = -1;`
var request2 = new RestRequest("https://myurl.com/api/site/Create", Method.Post);
request2.AddHeader("Authorization", "Bearer "+ _authstr);
// AddFile below, therefore commented out
// request2.AlwaysMultipartFormData = true;
request2.AddParameter("PFolderId", "a1c1w000000GR5hAAG");
request2.AddParameter("Title", "MyTitle5");
request2.AddParameter("GeoRegion", "{E|BE|BRUXELLES-BRUSSEL}");
request2.AddParameter("SeismicZone", "0");
request2.AddFile("file", "C:/Users/abc/Desktop/Drawing.png");
RestResponse response2 = client.Execute(request2);