0

I'm developing a simple REST service using Spring. I've a entity and a controller to that. My problem is that I can't use Post function from browser, it's just works from terminal. The Get function works fine from browser and terminal, but Post function just works from terminal, but I must that it works from browser.

For the code below, if I navigate to:

http://localhost:8080/cities

the result is ok, all records are returned.

Get method:

@RestController public class CityController { ... @GetMapping(value = "/cities", produces = "application/json; charset=UTF-8") List<City> all() { return repository.findAll(); } } 

For the Post method, just works from terminal if I write something like:

curl -X POST localhost:8080/cities -H 'Content-type:application/json' -d '{"name":"test", "state":"test"}'

Result is ok, record is created.

But, from browser, if I tries add a new record with:

http://localhost:8080/cities?name=test&state=test

nothing happens, and no error occurs.

Post method:

@PostMapping(path = "/cities", consumes = "application/json", produces = "application/json") City newCity(@RequestBody City city) { return repository.save(city); } 

Entity:

@Entity public class City { @Id @GeneratedValue(strategy=GenerationType.AUTO) Long id; private String name; private String state; public City() { } public City(String name, String state) { this.name = name; this.state = state; } public void setId(Long id) { this.id = id; } public Long getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getState() { return state; } public void setState(String state) { this.state = state; } } 
7
  • in google chrome add postman extension Commented Mar 22, 2019 at 14:32
  • So, it's impossible works on all browsers and no extensions? Commented Mar 22, 2019 at 14:32
  • How are you sending it as a POST from the browser? Just adding query parameters to the URL doesn't make it a POST. Commented Mar 22, 2019 at 14:33
  • So, how I do it? Commented Mar 22, 2019 at 14:34
  • stackoverflow.com/questions/4797534/… Commented Mar 22, 2019 at 14:34

1 Answer 1

1

Typing http://localhost:8080/cities?name=test&state=test into a browser is still going to send it as a GET.

To send as a POST, you have a few options:

  1. Use a browser plugin as others have mentioned.
  2. Create an HTML form.
  3. Use JavaScript.

Option 1 is great for debugging and testing, but is no way appropriate for a production quality web site. You cannot reasonably expect your visitors to install or use a browser add-on to interact with your site.

Option 2 is the most traditional design. You would need to serve a HTML file from your application (it can be a static HTML file or use a template framework such as Thmyeleaf or Freemarker). The HTML would need a form element that is configured to use POST and point it back to your endpoint. Keep in mind your endpoint would need to accept form encoded data, not just JSON.

Option 3 could be implemented in several ways. You could have a HTML file that uses embedded JavaScript to call your endpoint, or you could use some framework like Angular or React.

Lots of options, and it's hard to say which one is best without knowing what exactly you're trying to accomplish.

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

3 Comments

Thank you, you explanation was helpful to solve my problem.
Question: I can make a DELETE request using browser like use GET?
@Augusto Only GET and POST methods are allowed in native HTML forms. For PUT or DELETE you would need to use JavaScript, or you could write a new POST endpoint in your app that really does delete, but this is counter to the HTTP spec and I wouldn't recommend it. See softwareengineering.stackexchange.com/a/211790 for some background.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.