Hi
I have below code & i don't want to write in every Program. It should be written once . How it can be done
Parameters should be passed to the calling program and respose returned by Api should be displayed
private void GetCourier(string token)
{
try
{
var client = new RestClient("htts://api.nimbuspost.com/v1/courier");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer " + token);
request.AddParameter("origin", txtWeight.Text, ParameterType.RequestBody);
request.AddParameter("destination", txtLength.Text, ParameterType.RequestBody);
request.AddHeader("content-type", "application/json");
IRestResponse response0 = client.Execute(request);
Root root = JsonConvert.DeserializeObject(response0.Content);
foreach (var item in root.data)
{
ddlCourier.Items.Add(new ListItem(item.name, item.id));
}
}
catch (Exception ex)
{
Response.Write(ex.Message + "CreateToken");
}
}
Amit MohantyPosted Jan 9, 2023, 10:00 AM
Hey Ramco,
you have already mentioned Root class in your question. Use the same Root class.
Ramco RamcoPosted Jan 9, 2023, 9:58 AM
Hi Sachin
ON below line i am getting error - Using the generic type JsonResult requires 1 type arguments
public JsonResult GetCourier(string token, Int32 origin, Int32 destination,string PaymentType,Int32 OrderAmount)
Thanks
Ramco RamcoPosted Jan 9, 2023, 9:46 AM
Hi Amit
What is Root. How it is to be used.
Thanks
Aravind GovindarajPosted Jan 9, 2023, 9:02 AM
Typically you need to follow a few things here, such as
ExternalInterface Library => ( This is the gateway to connect the external system)
Under the API Folder, you have to create Response and Request. The request is the one which will till this part
once done Get the Response
one more function to format as per the required format response
Finally call this library and method from source , you can reuse this anywhere even for calling other api / wcf/ web service
Pasang TamangPosted Jan 9, 2023, 8:41 AM
Hi,
You can create a function to re-use same code. Please check below code example.
And this function you can call like in below code
Regards,
Pasang
Sachin SinghPosted Jan 9, 2023, 8:07 AM
You shouldn't return/add ddlcourier from a common method, rather do this
Amit MohantyPosted Jan 9, 2023, 7:40 AM
try this
Dropdownlist class
uses
Ramco RamcoPosted Jan 9, 2023, 7:36 AM
Hi
Sorry Sachin. I want for this below part . Parameters (Origin & Destination) should be passed & then Json result should be returned
Thanks
Sachin SinghPosted Jan 9, 2023, 7:22 AM
make the method public and return JsonResult instead