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;
    }

}
Augusto
  • 3,825
  • 9
  • 45
  • 93
  • in google chrome add `postman` extension – Ryuzaki L Mar 22 '19 at 14:32
  • So, it's impossible works on all browsers and no extensions? – Augusto Mar 22 '19 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`. – Mike Mar 22 '19 at 14:33
  • So, how I do it? – Augusto Mar 22 '19 at 14:34
  • https://stackoverflow.com/questions/4797534/how-do-i-manually-fire-http-post-requests-with-firefox-or-chrome – Ryuzaki L Mar 22 '19 at 14:34
  • Possible duplicate of [How do I manually fire HTTP POST requests with Firefox or Chrome?](https://stackoverflow.com/questions/4797534/how-do-i-manually-fire-http-post-requests-with-firefox-or-chrome) – Ryuzaki L Mar 22 '19 at 14:34
  • @Augusto, what do you mean from browser? Hitting the url makes `HTTP GET` request. To make a `POST` you should call your api from `Javascript`. Another option is to use `HTML` forms if applicable (`Content-Type: application/x-www-form-urlencoded` will be send to the server). If you want to call your api just for the sake of testing you could use different browser extensions or tools like postman – StasKolodyuk Mar 22 '19 at 14:44

1 Answers1

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.

Mike
  • 4,722
  • 1
  • 27
  • 40
  • 1
    Thank you, you explanation was helpful to solve my problem. – Augusto Mar 22 '19 at 16:28
  • Question: I can make a `DELETE` request using browser like use `GET`? – Augusto Mar 23 '19 at 14:12
  • 1
    @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 https://softwareengineering.stackexchange.com/a/211790 for some background. – Mike Mar 23 '19 at 14:47