0

I have the code below:

Here is my MVC Action Method

    [HttpPost]
    public ActionResult SaveOrder(myObject obj)
    {                     
       HelperClass.CreateOrder(obj.orderid, obj.ordername, out response);       
    }

In my helper class is the CreatOrder function as follows:

    public static object CreateOrder(string orderid, string ordername, out bool response)
    {
        response = false;
        try
        {
            var client = new RestClient();
            var request = new RestRequest("url", Method.POST);
            request.AddHeader("Authorization", "Bearer " + "mytoken");
            var body = new
            {
                orderid = orderid,
                ordername = ordername                    
            };
            request.AddJsonBody(body);
            var response = client.Execute(request); <---- this execute gives Status 0

            .........                
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Is this possible since i am already making a HttpPost from within the Action method ? How can i do the Api call and get back the result ?

ADyson
  • 57,178
  • 14
  • 51
  • 63
Debbie.S
  • 167
  • 11
  • Yes you can make a HTTP request from within an action method, or from anywhere else in a C# program. If the request isn't working properly, the cause is not because it was invoked from within an action method. – ADyson Jun 05 '20 at 07:28
  • strange , same call in the Action method is made from postman and it returns a OK result ? – Debbie.S Jun 05 '20 at 07:52
  • Evidently something is different between the HTTP requests being generated in those two scenarios then. But we don't have any information with which to tell you what the differences might be. The pure fact of it being called from an action method isn't the cause. Maybe make a small Console app to test just this REST call code, then it's easier to test, and you can see that it isn't the action method by itself which is at fault. You can also do things like use a tool such as Fiddler to see the HTTP requests generated in each case, then compare them and adjust your C# accordingly – ADyson Jun 05 '20 at 07:56
  • Tested in a standalone app . it returns a valid response. If i back track what actually cals the Action Method, From my angular controller I call a service method , which in turn invokes the Actiuon Method. If i debug the response i get the following"The underlying connection was closed: An unexpected error occurred on a send., An existing connection was forcibly closed by the remote host" . Do i need to spin up another thread to run the api call in , as i have no other calls to the endpoint ? – Debbie.S Jun 05 '20 at 08:36
  • "The underlying connection was closed" ... to be clear, this exception is generated by the RestSharp code, or somewhere else? I need to know _which_ connection is being closed (the one to the remote server, or the one between your browser and your .net application). Also where are you running this code? If you're running it on a different machine to where the PostMan and console app are running, then it's possible that something is blocking the connection to the remote server. – ADyson Jun 05 '20 at 08:44
  • The error says that the connection to the remote server is being closed. all apps console ,postman are run on the same machine . – Debbie.S Jun 05 '20 at 08:52
  • ok well another thing that occurred to me: have you tried hard-coding the MVC version of the restsharp code with exact values for the parameters you're sending? I would guess you did this for the console version? So try repeating that in MVC, in case the sent values are somehow causing an issue. Try to use the **exact** same restsharp code in console and MVC versions. This is a way we can rule in or out some of the different factors in the environment – ADyson Jun 05 '20 at 08:55
  • versions of restsharp are all the same , and values have been hardcoded as in the working console app. So what im looking at doing is making an api cal from within the angular controller as this is clearly not working for some unknow reason .. – Debbie.S Jun 05 '20 at 09:18
  • Just bear in mind that the remote endpoint would need to support CORS for that to work – ADyson Jun 05 '20 at 09:34
  • 1
    P.S. re the RestSharp error, don't know if any of these answers would be relevant: https://stackoverflow.com/questions/48549161/api-is-returning-error-when-using-restsharp, https://stackoverflow.com/questions/21447459/restsharp-the-underlying-connection-was-closed-a-connection-that-was-expected, https://github.com/restsharp/RestSharp/issues/942 and others. (I just googled "restsharp the underlying connection was closed: An unexpected error occurred on a send., An existing connection was forcibly closed by the remote host" and there is quite a lot of info around) – ADyson Jun 05 '20 at 09:38
  • @ADyson works perfectly now with the following link you provided. https://stackoverflow.com/questions/48549161/api-is-returning-error-when-using-restsharp – Debbie.S Jun 08 '20 at 11:24

0 Answers0