0

I want to know how to test POST by typing in the url. Here's my route Config

config.Routes.MapHttpRoute(
                 name: "myWebApi",
                 routeTemplate: "api/mywebapi/{action}/{ID}/{DeptID}",
                 defaults: new { Controller = "mywebapi", ID = @"\d+", DeptID = @"\d+" }
             );

programmatically this is how I call POST I have 3 text boxes and a button. When user clicks on the button the below program gets called

function parseform(button) {
            var id = $("#ID").val();
            var deptid = $("#DeptID").val();
            var name = $("#Name").val();

            var inputdata = {
                id: id,
                deptid: deptid,
                name: name
            }

            if (button.attr('value') === "POST") {
                postdata(inputdata);
            } else {
                console.log("ERROR");
            }

        }
 function postdata(inputdata) {
            $("#response").text("Posted");
            $.ajax({
                type: "POST",
                dataType: "json",
                url: "api/mywebapi/Post/",
                contentType: "application/json",
                data: JSON.stringify(inputdata),
                xhrFields: {
                   withCredentials: true
                },
                success: function (data, status, xhr) {

                   $("#response").text(status+" - "+data)
                },
                error: function (xhr, status, error) {

                    var json = jQuery.parseJSON(xhr.responseText);

                    $("#response").text(status)

                }
            });
        }

In the controller

[System.Web.Http.AcceptVerbs("POST")]
        public void Post([FromBody]mywebapi value)
        {
        saves to database
        }                

Here's what I tested

http://localhost:222/api/mywebapi/Post/new newwebapi ({"id":"1","deptid":"2","name":"testing"})

I get error. How to test this?

thanks R

1 Answer 1

1

Since it's a POST request, you can't test it in your browser by typing in an address (those are GET requests, which contain no body).

To test these types of things you can use something like Postman or Rest Console (if you're using chrome), there's tons of these types of things in whatever your browsers extension store is called.

Some tools you can use are something like Fiddler

this will let you see what the requests and responses look like, and you can change/modify them as well, though it's probably a bit harder to use than something like PostMan or Rest Console (also more powerful)

Sign up to request clarification or add additional context in comments.

2 Comments

For completeness - there are also console tools around, like curl or wget
Can I call a Post from Get function??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.