how to test apiController
Asked Answered
I

2

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

Interrelated answered 5/1, 2018 at 5:24 Comment(4)
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 doErythropoiesis
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?Apparel
im reading online about routes do I have to use these?Interrelated
@ChetanRanpariya I dont kno how to make a request to an api im reading up on postman atmInterrelated
M
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.

Mariettemarigold answered 5/1, 2018 at 6:25 Comment(3)
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 127.0.0.1/SynchronisePersonnelXmlFromAwsServiceController/… in the postman UIInterrelated
I have tried what running play from VS and posting to 127.0.0.1/SynchronisePersonnelXmlFromAwsServiceController/… but this has returned <title>Failed to start monitoring changes to 'D:\Development\Trunk\WebAppsMVC\Icon\global.asax' because access is denied.</title>Interrelated
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 notMariettemarigold
U
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
Uredo answered 5/1, 2018 at 5:31 Comment(4)
I found an example that uses restSharp from the last comment on the Q #10226589 and forgive my simple Q but the code used in pawel.sawicz.eu/restsharp ....where do I add this too ? do I just create a new class?Interrelated
Create a class and add the method which will call your api using rest sharp.Uredo
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<List<Items>>(request).Data; but <Items> is flagging as not found...what is items in this instance?sorry if its something silly is it a ref im missing?Interrelated
try following code. I have created console application link, for Item you need to use JsonConvert.DeserializeObject<T>(response.Content); using Newtonsoft nuget package.Uredo

© 2022 - 2024 — McMap. All rights reserved.