Search code examples
c#.netrestiiscontroller

How to call POST API method which is hosted on IIS


I have a following http post and GET methods in MVC API Transaction controller;

    [HttpPost]
    public void ImportTransactions()
    {
      // import transactions

    }

 [HttpGet]
    public IHttpActionResult IsApiAlive()
    {
        return Ok();
    }

API service is hosted on IIS, when i run the following URL in IE or Chrome to POST data then some how service is calling GET method not post method which is odd;

http://myserver01/Ftransactions/api/Transaction/ImportTransactions

URL works fine in postman when i select POST option from the dropdown.


Solution

  • If you're sending requests trough your bowsers URL bar or by clicking a link it will always be a GET request. To have further request options like POST you need to use an HTML form or AJAX call (javascript) or have have a fancy little browser plugin installed.

    E.g.:

        <form action="/Ftransactions/api/Transaction/ImportTransactions" method="post">
              Data: 
              <input type="text" name="data">
              <input type="submit" value="Submit">
        </form> 
    

    Most browsers have something like a dev-tool which can often be started by pressing F12 on your keyboard or via the context menu (right click -> inspect). This allows to track the requests you're sending so you can see there of what type your requests are.

    Please also mind the missing I in your methods name mportTransactions()