Change transition in jira using c# - c#

I'm trying to change status in jira using c# by put method.
in the rest API documentation I've saw that i need to connect like this:
/rest/api/3/workflow/transitions/{transitionId}/properties'
but i i'm not sure of what parameters to change in the line.
how do i find the transition id..
or should i change the word properties with some property..
i'm a fresh student so be Gentle with me.
I have searched all over the web and also tried to ask one of my teachers and a fellow programmer.
this is the code i have written/copied from the web:
Jira jira = Jira.CreateRestClient("http://localhost:8080/", "username", "password);
HttpWebRequest request = WebRequest.Create("http://localhost:8080/") as HttpWebRequest;
request.ContentType = "application/json";
request.Method = "GET";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
var projectKey = "CLEARN";
string base64Credentials = GetEncodedCredentials();
var client = new RestClient("http://localhost:8080/rest/api/3/workflow/transitions/{transitionId}/properties' ");
var request2 = new RestRequest(Method.PUT);
request2.AddHeader("postman-token", "BL0C-B43C-1ZWZ-NQUW_68b5a15fbd28c0a42df2cb47e8f7831155704df9");
request2.AddHeader("cache-control", "no-cache");
request2.AddHeader("Authorization", "Basic " + base64Credentials);
request2.AddHeader("content-type", "application/json");
request2.AddParameter("application/json", "\r\n{\r\n \"update\": {},\r\n \"transition\": {\r\n \"id\": \"10000\"\r\n },\r\n \"fields\": {\r\n \"resolution\": {\r\n \"name\": \"Done\"\r\n }\r\n }\r\n}", ParameterType.RequestBody);
IRestResponse response2 = client.Execute(request2);
for now i get forbidden status code when i'm trying to change values in the url.

this the correct code that made the correct change:
"
string base64Credentials = GetEncodedCredentials();
var client = new RestClient("http://localhost:8080/rest/api/2/issue/clearn-1/transitions/");
var request2 = new RestRequest(Method.POST);
request2.AddHeader("postman-token", "2f8d5f8e-c7a2-4dd6-acc3-2f992f244455");
request2.AddHeader("cache-control", "no-cache");
request2.AddHeader("Authorization", "Basic " + base64Credentials);
request2.AddHeader("content-type", "application/json");
request2.AddParameter("application/json", "\r\n{\r\n \"update\": {},\r\n \"transition\": {\r\n \"id\": \"41\"\r\n }}", ParameterType.RequestBody);
"

Related

Getting a NOT.AUTHORIZED.ERROR when trying to call fedex api tracking

I am trying to connect and pull tacking data from FedEx using there API.
I have been successful in connecting to and getting a response from https://apis-sandbox.fedex.com//oauth/token and getting the token.
When I then try to connect and get the tracking data I am getting "NOT.AUTHORIZED.ERROR" error response.
string input = "{\r\n \"trackingInfo\": [\r\n {\r\n \"trackingNumberInfo\": {\r\n \"trackingNumber\": \"794843185271\"\r\n }\r\n }\r\n ],\r\n \"includeDetailedScans\": true\r\n}";
var client = new RestClient("https://apis-sandbox.fedex.com");
var request = new RestRequest("/oauth/token",Method.Post);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&scope=all&client_id=MyClientId&client_secret=MyClientSecert", ParameterType.RequestBody);
RestResponse response = client.Execute(request);
Console.WriteLine(response.Content.ToString());
Console.WriteLine(" ");
Console.WriteLine("#########################");
Console.WriteLine(" ");
rff = JsonSerializer.Deserialize<ResponceFromFedex>(response.Content);
string token = rff.access_token;
request = new RestRequest("/track/v1/trackingnumbers", Method.Post);
request.AddHeader("Authorization", "bearer " + token);
request.AddHeader("X-locale", "en_US");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", input, ParameterType.RequestBody);
response = client.Execute(request);
Console.WriteLine(response.Content.ToString());
Console.ReadLine();
I am very new to this API stuff and have no idea what I am doing, Any help would be greatly appreciated
I should add that I have been able to make the call using PostMan, I just don't know to to translate that to C#
I have tried tweaking setting and have not been able to get a proper response
To finaly get this to work I had to add a Default Query Parameter to the client
client.AddDefaultQueryParameter("access_token", token);
Here is a full look at the code
string input = "{\r\n \"trackingInfo\": [\r\n {\r\n \"trackingNumberInfo\": {\r\n \"trackingNumber\": \"794843185271\"\r\n }\r\n }\r\n ],\r\n \"includeDetailedScans\": true\r\n}";
var client = new RestClient("https://apis-sandbox.fedex.com");
var request = new RestRequest("/oauth/token",Method.Post);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&scope=all&client_id=MyClientId&client_secret=MyClientSecert", ParameterType.RequestBody);
RestResponse response = client.Execute(request);
Console.WriteLine(response.Content.ToString());
Console.WriteLine(" ");
Console.WriteLine("#########################");
Console.WriteLine(" ");
rff = JsonSerializer.Deserialize<ResponceFromFedex>(response.Content);
string token = rff.access_token;
request = new RestRequest("/track/v1/trackingnumbers", Method.Post);
//Line of code that was added.
client.AddDefaultQueryParameter("access_token", token);
request.AddHeader("Authorization", "bearer " + token);
request.AddHeader("X-locale", "en_US");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", input, ParameterType.RequestBody);
response = client.Execute(request);
Console.WriteLine(response.Content.ToString());
Console.ReadLine();

WebAPI call using RestSharp gives status 404; WebRequest succeeds

I've got a section of code to do a call to an external webapi using WebRequest. I'm trying to update it to use RestSharp instead. What am I missing here to make the conversion? The closest question here to give any sort of idea what might be going on is found in Calling webapi method gives 404. The "answer" was a missing accepted content type. I've verified through the RestClient the types are present (and appear valid).
Common request JSON
var statusRequest = #"
{
""auth"": {
""type"": ""basic""
},
""requestId"": ""15"",
""method"": {
""name"": ""getStatus"",
""params"": {
""showAllStatus"": ""0""
}
}
}
";
WebRequest code
var webRequest = WebRequest.Create("https://service.url/api/status") as HttpWebRequest;
webRequest.Method = "POST";
var username = "user";
var password = "pass";
var encoded = System.Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1")
.GetBytes(username + ":" + password));
webRequest.Headers.Add("Authorization", "Basic " + encoded);
var requestWriter = new StreamWriter(webRequest.GetRequestStream());
webRequest.ContentType = "APPLICATION/JSON; CHARSET=UTF-8";
requestWriter.Write(statusRequest);
requestWriter.Close();
var responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
var responseData = responseReader.ReadToEnd();
responseReader.Close();
Should convert to RestSharp as
var client = new RestClient("https://service.url");
client.Authenticator = new HttpBasicAuthenticator("user", "pass");
var request = new RestRequest("api/status", Method.Post);
request.RequestFormat = DataFormat.Json;
request.AddJsonBody(statusRequest);
client.BuildUri(request);
var response = await client.GetAsync(request);
EDIT: Make the RestRequest a POST
Please read the docs.
Also, you are calling GetAsync. Even if you set the request method to Method.Post, calling GetAsync will override it.
This will work:
var request = new RestRequest("api/status")
.AddStringBody(statusRequest, DataFormat.Json);
var response = await client.PostAsync(request);

Post Http call working on postman but code not working in C#

I have an issue with postman and c# code. I have two post calls to an API that must in the end make a callback to another API (webhook).
I tried to launch the two calls through Postman and i do obtain correctly the callback response. My issue is that when I use this code I do not have any callback response but I obtain 200 message from the server I call. I have the same issue with all implementations of the http post calls I sent and get the same issue.
public static void Main(string[] args)
{
// first call
var client = new RestClient("http://xxxxxxx/emails/attachments/");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization",
"Bearer theToken");
request.AddFile("attachment",
"C:/Users/..../pdf.pdf");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
var attachmentId = response.Content;
// second call
client = new RestClient("http://xxxxxxx/emails/");
client.Timeout = -1;
request = new RestRequest(Method.POST);
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization",
"Bearer theToken");
request.AddParameter("application/json",
"{\r\n \"subject\": \"email with attachment\",\r\n \"body\": \"Hi !!!l\\r\\n\",\r\n \"received_on\": \"2021-05-24T14:07:01.5874416+02:00\",\r\n \"author\": {\r\n \"name\": \"dvera#mymail.fr\",\r\n \"smtp_address\": \"\"\r\n },\r\n \"sender\": {\r\n \"name\": \"dvera#mymail.fr\",\r\n \"smtp_address\": \"\"\r\n },\r\n \"to\": [\r\n {\r\n \"name\": \"dvera#mymail.fr\",\r\n \"smtp_address\": \"\"\r\n }\r\n ],\r\n \"cc\": [\r\n {\r\n \"name\": \"\",\r\n \"smtp_address\": \"\"\r\n }\r\n ],\r\n \"attachments\": [\r\n " +
attachmentId + "\r\n ]\r\n}\r\n", ParameterType.RequestBody);
response = client.Execute(request);
Console.WriteLine(response.Content);
}
}
Any idea about what's going wrong ? the weird thing is that I have a 200 response for each calls i make.
I solved my issue with this code:
using (var client = new HttpClient())
{
using (var form = new MultipartFormDataContent())
{
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
var file = File.OpenRead(attachmentPath);
byte[] fileBytes = new byte[file.Length];
form.Add(new ByteArrayContent(fileBytes, 0, fileBytes.Length), "attachment", Path.GetFileName(attachmentPath));
HttpResponseMessage response = await client.PostAsync(url, form);
response.EnsureSuccessStatusCode();
var output = await response.Content.ReadAsStringAsync();
PostEmailAttachmentResponse returnValue = new PostEmailAttachmentResponse();
returnValue.Id = Int32.Parse(output);
return returnValue;
}
}
My previous code wasn't returning error message when i sent attachment. there was issue on the server side which was not returning error.

RestSharp Post Request Array Object

I've been looking for a way to use RestSharp C# to send POST request with the JSON below.
[{"Guid": "ccda117f-a9b5-4f54-ab4a-07a9cf403ef7"}]
It starts with the bracket and I don't see any reference similar to this.
Can you provide any link or sample code that I can use for reference?
Thanks!
var client2 = new RestClient("https://test.rest.com/api/v2/notifications/channels/" + channelId + "/subscriptions");
var request2 = new RestRequest(Method.POST);
request2.AddHeader("cache-control", "no-cache");
request2.AddHeader("authorization", "Bearer " + access_token);
request2.AddHeader("Content-Type", "application/json");
var myJson = "[{\"Guid\": \"ccda117f-a9b5-4f54-ab4a-07a9cf403ef7\"}]";
request2.AddParameter("application/json", myJson, ParameterType.RequestBody);
IRestResponse response2 = client2.Execute(request2);

What the difference between this Post webrequest and similar request in Postman?

I want to make Post request to "https://sslecal2.forexprostools.com/ajax.php". So there is my code:
string URI = "https://sslecal2.forexprostools.com/ajax.php";
string requestBody = String.Format("{{\"dateFrom\": \"{0}\", \"dateTo\": \"{1}\", \"timeZone\": {2}, \"action\": \"{3}\"}}",
"2018-12-24", "2018-12-24", 18, "filter"); //json format
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URI); //make request
request.Method = "POST";
request.UserAgent = "";
request.Headers.Add("X-Requested-With", "XMLHttpRequest");
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(requestBody); //write your request payload
}
WebResponse response = request.GetResponse();
string jsonData = String.Empty;
using (var reader = new StreamReader(response.GetResponseStream()))
{
jsonData = reader.ReadToEnd();
}
response.Close();
I made something not correct in "requestBody" in string " string requestBody = String.Format("{{\"dateFrom\"..." because I get 200 and empty html answer.
And I attach the screens of the same request in postman with html code in answer. This request in postman processes well.
What the difference between this Post webrequest and request in Postman?
With postman you posting different format data. To get same thing in code you need to change request body format and set content type of request:
string URI = "https://sslecal2.forexprostools.com/ajax.php";
string requestBody = String.Format("dateFrom={0}&dateTo={1}&timeZone={2}&action={3}",
"2018-12-24", "2018-12-24", 18, "filter"); //<-- Change this
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URI);
request.Method = "POST";
request.UserAgent = "";
request.Headers.Add("X-Requested-With", "XMLHttpRequest");
request.ContentType = "application/x-www-form-urlencoded"; //<-- Add this
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(requestBody);
}
WebResponse response = request.GetResponse();
string jsonData = String.Empty;
using (var reader = new StreamReader(response.GetResponseStream()))
{
jsonData = reader.ReadToEnd();
}
response.Close();
In PostMan, if you click the "Code" in the top right, under the send button, you can choose C# (RestSharp).. If you're not using RestSharp, there's a small amount of work to do to convert it to something else, but the basics are all there.
Here's the autogen output for your case (RestSharp):
var client = new RestClient("https://sslecal2.forexprostools.com/ajax.php");
var request = new RestRequest(Method.POST);
request.AddHeader("Postman-Token", "bfd1a3b3-983f-4160-a091-6f0962413e58");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("X-Requested-With", "XMLHttpRequest");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("undefined", "dateFrom=2018-01-24&dateTo=2018-01-24&timeZone=18&action=filter", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Converting it to HttpWebRequest requires:
AddHeader -> Headers.Add
Specify method
Body data is set differently - take PostMan's string and write it to the request stream
Or install RestSharp free from NuGet

Categories