Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Let's say that I want to create a website where users will be able to create, edit and delete some kind of resources, for example posts.

I have created a RESTful API, so user can create a post by sending a PUT request to http://www.example.com/posts/my-new-awesome-post, delete it by sending DELETE request, etc.

But most users don't know anything about HTTP requests. They want to click a "New post" button and have the new post created. But HTML only allows me to use GET and POST.

How can I create a "New post" button that will send a PUT request? Or is "RESTful creation/deletion" implemented in a totally different way?

Let's say I'm using Spring + Thymeleaf.

share|improve this question
3  
HTML pages do not normally interact directly with REST APIs. Those are meant for web service clients to use, although I suppose you could have HTML in a browser be the client. I think you need to take a step back and reevaluate your overall system design. –  Snowman Jul 10 at 14:51
    
What's wrong wtih POST? –  JayMee Jul 10 at 15:40
    
@JayMee religious objections, from those who feel that if you're not using PUT, DELETE, etc., your API is wrong –  Carson63000 Jul 11 at 2:48
    
Rest is an anti pattern –  Ewan Jul 13 at 17:24
    
@Ewan Could you please elaborate why? –  infranoise Jul 23 at 10:49

1 Answer 1

up vote 4 down vote accepted

The 'New Post' button would go to a form for the user to input the information for a new post. When the user submits that form the browser rightly sends that data via HTTP POST (use POST for create).

An edit submission would use PUT or PATCH depending on whether it is a replace (delete the missing properties) or update (only change the submitted properties), respectively.

We usually make an XMLHTTPRequest (XHR) using JavaScript for events other than GET or POST.

jQuery.ajax('/products/42', {method: 'PUT', data: product_data});

jQuery Docs: http://api.jquery.com/jquery.ajax/

Additionally, Rails will use a '_method' param in POSTs to force a different action on the server ( like PUT or PATCH).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.