in Education by
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 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; } } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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: Use a browser plugin as others have mentioned. Create an HTML form. 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.

Related questions

0 votes
    I'm trying to make it so when a user scrolls down a page, click a link, do whatever it is they ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 12, 2022 in Education by JackTerrance
0 votes
    Why is it not a good idea to use SOAP for communicating with the front end? For example, a web ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    Why is it not a good idea to use SOAP for communicating with the front end? For example, a web ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    Why is it not a good idea to use SOAP for communicating with the front end? For example, a web ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I want my browser to open my local images and to place them at the center of my browser. The ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 20, 2022 in Education by JackTerrance
0 votes
    How will the HTTP GET request be sent from the browser? (a) Remote server (b) Local server (c) ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    I'm trying to incorporate Google Maps into my Access form so that for every record a map of its ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 13, 2022 in Education by JackTerrance
0 votes
    Spywares can be used to steal _______________ from the attacker's browser. (a) browsing history (b) company details ... questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 2, 2021 in Education by JackTerrance
0 votes
    If you've accidentally clicked any pop-up which seems malicious, it is recommended to take steps to remove ... for-Cyber Security:,Cyber Security-Jobs:,Cyber Security Applications...
asked Oct 31, 2021 in Education by JackTerrance
0 votes
    A __________ is a program running on the server machine, which accepts requests from a Web ... Fundamentals in division Database Programming Techniques of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    User visits subdomain1.mydomain.com, which sends back cache-control (public, max-age=86400) instructions in ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    User visits subdomain1.mydomain.com, which sends back cache-control (public, max-age=86400) instructions in ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    If you call javascript window.open and pass a url to a .xls file it open on some machines in the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    I have noticed that some browsers (in particular, Firefox and Opera) are very zealous in using cached ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
...