0

Im working on an existing Windows Service project in VS 2013.

I've added a web API Controller class I cant remember now if its a (v2.1) or (v1) controller class....Anyway I've called it SynchroniseFromAwsService

Im trying to call it from a AWS lambda call but it is telling me I dont have access. So I need to test it locally to see if it is working to try and diagnose the issue.

I want to test this locally but am unsure how to do so..please see code...

 namespace Workflow
{
    public class SynchroniseFromAwsService: ApiController
    {
        //// POST api/<controller>
        public string SapCall([FromBody]string xmlFile)
        {
            string responseMsg = "Failed Import User";

            if (!IsNewestVersionOfXMLFile(xmlFile))
            {
                responseMsg = "Not latest version of file, update not performed";
            }
            else
            {
                Business.PersonnelReplicate personnelReplicate = BusinessLogic.SynchronisePersonnel.BuildFromDataContractXml<Business.PersonnelReplicate>(xmlFile);
                bool result = Service.Personnel.SynchroniseCache(personnelReplicate);

                if (result)
                {
                    responseMsg = "Success Import Sap Cache User";
                }
            }

            return "{\"response\" : \" " + responseMsg + " \" , \"isNewActiveDirectoryUser\" : \" false \"}";
        }
}
}

I've read on google to download a program called Postman and test it.

Is there nothing I can call in VS and just pass in a dummy data string containing an xml file to test?

What would be the best way

thank you for any replied

John
  • 3,965
  • 21
  • 77
  • 163
  • you can create a post request to your api service, to test. Things will be simple with postman, or just right a simple html file will do – Jacky Jan 05 '18 at 05:27
  • If you know how to make a request to web api then you should be able to do it easily by using postman. Can you tell us what behavior you ar3 getting when try to call api from posyman? Did you try browsing url of the api from Google chrome? – Chetan Jan 05 '18 at 05:28
  • im reading online about routes do I have to use these? – John Jan 05 '18 at 05:28
  • @ChetanRanpariya I dont kno how to make a request to an api im reading up on postman atm – John Jan 05 '18 at 05:29

2 Answers2

2

Your code is right. You need to test it locally is it. You can just accomplish it using Postman rest client itself. It's very simple.
In your code the HTTP method attribute is missing for your function SapCall. So, specify attribute for your method used to identify which type of request is this.

[HttpPost]
public string SapCall([FromBody]string xmlFile){

Now use postman rest client and call ur API url. It will get execute successfully.

Clinton Prakash
  • 967
  • 9
  • 20
  • thanks for the reply OK iv looked over a postMan tutorial it seems easy enough. May I ask though how do I just call this do I need to setup the VS through IIS or can I just hit the play button and try the post request for http://127.0.0.1/SynchronisePersonnelXmlFromAwsServiceController/SapEaiCall in the postman UI – John Jan 05 '18 at 06:47
  • I have tried what running play from VS and posting to http://127.0.0.1/SynchronisePersonnelXmlFromAwsServiceController/SapEaiCall but this has returned Failed to start monitoring changes to 'D:\Development\Trunk\WebAppsMVC\Icon\global.asax' because access is denied. – John Jan 05 '18 at 06:50
  • Whether it is a Web API project? If it is a Web API project. Just hit the play button and try the request to 127.0.0.1/SynchronisePersonnelXmlFromAwsServiceController/… from the postman UI. If the issue still exists. Just implement a sample HTTP GET request method in your controller and call the request from postman UI and check whether the connection is successful or not – Clinton Prakash Jan 05 '18 at 08:59
1

you can use

RestSharp Simple REST and HTTP API Client for .NET

visit http://restsharp.org/ for more details.

using nuget package manager install this dependency in your project

Install-Package RestSharp -Version 106.2.0
Suraj
  • 345
  • 1
  • 2
  • 12
  • I found an example that uses restSharp from the last comment on the Q https://stackoverflow.com/questions/10226089/restsharp-simple-complete-example and forgive my simple Q but the code used in http://pawel.sawicz.eu/restsharp/ ....where do I add this too ? do I just create a new class? – John Jan 05 '18 at 05:47
  • Create a class and add the method which will call your api using rest sharp. – Suraj Jan 05 '18 at 05:53
  • thank you for reply iv created a new class and added a function with the following code C# var client = new RestClient("192.168.0.1"); var request = new RestRequest("api/item/", Method.GET); var queryResult = client.Execute>(request).Data; but is flagging as not found...what is items in this instance?sorry if its something silly is it a ref im missing? – John Jan 05 '18 at 06:08
  • try following code. I have created console application [link](https://dotnetfiddle.net/jLgdda), for `Item` you need to use `JsonConvert.DeserializeObject(response.Content);` using `Newtonsoft` nuget package. – Suraj Jan 05 '18 at 06:17