Rob
Rob

Reputation: 3084

Web API GET vs POST

I initially setup a Web API (not any expert with web api) and followed someone's tutorial online for passing in parameters to a simple get API call.

I had originally created a GET with query strings but as the tutorial showed how using POST allows me to pass JSON to an class object in POST API parameter, it seemed a good idea.

Later on one developer said this was bad practice? Is it? Should I always use GET instead of POST which in essence is what it should be anyway... a GET call, but I like the idea of passing parameters via an object and avoiding a long API call with query strings.

So:

$.ajax({
    url: '\api\getlist\1?param2=yyyy&param3=kikkkk&param4=88' etc   
})

or

var params = ....

$.ajax({
    url: '\api\getlist\',
    data: params
})

What should I do, change the code back to using GET? What about caching?

Upvotes: 3

Views: 8007

Answers (1)

Felipe Oriani
Felipe Oriani

Reputation: 38608

You should take a look at the http documentation in w3c.

GET is recommended for reading data and POST is used to send information (write operations) into the server.

From client side perspective, you could set in jquery ajax setup, to do not use cache with the follow code:

$.ajaxSetup({
    cache: false
});

It will generate a random argument on the async request to make a different request everytime, add a parameter like this: ?_=31312312312.

Upvotes: 3

Related Questions