简体   繁体   中英

Should I store _method=PUT/DELETE in the post or in the url

I'm using ASP.NET MVC to build a RESTful web application and I plan to tunnel PUT and DELETE requests through POST as it seems like the most pragmatic workaround.

What I'd like to know is, should I tunnel the information through the url like this:

<form method='post' action='resource?_method=DELETE'>
    <!-- fields -->
</form>

Or should I tunnel it through the posted form data like this:

<form method='post' action='resource'>
    <input type='hidden' name='_method' value='DELETE' />
    <!-- fields -->
</form>

And what are the pros and cons of each?

EDIT: One of the reasons I asked the question is that I read somewhere that putting information like this in the url is a good thing as post data usually gets lost, but urls hang around (in log files, etc) - unfortunately it makes the url look ugly

Have you seen this question? From what I understand, the x-http-method-override header is the preferred solution to this problem.

It's more of personal preference. Restful Web Services, OReilly, describes both somewhat interchangeably.

That being said, I prefer the first method for reasons of programmer intent. In Rest, when I'm looking at the code, I mentally read

VERB http://someuri.com/someresource/id

The verb and the resource are close together.

With PUT and DELETE you have to use a workaround like the ones that you've shown. In the first example, the resource and the verb are still close together on the same line.

In your second example, however, the resource is split into two lines. The verb is included on the same line as the resource id, but away from the resource name. It's very, very minor, but to me, that makes the second example less readable.

Not that I have, but shouldn't you use:

<form method="put" action="resource">
    <!-- fields -->
</form>

And / or

<form method="delete" action="resource">
    <!-- fields -->
</form>

...?

They are equivalent.

Though if pressed I'd prefer sending it in the post myself, but it's subjective.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM