-2

Could anyone tell me how to call post request from simple terminal client?

    @POST
    @Path("insertt")
    public void insert(@QueryParam("id") Integer id,
                       @QueryParam("name") String name,
                       @QueryParam("lastname") String lastname,
                       @QueryParam("adress") String adress,
                       @QueryParam("city") String city ) {
        Customer cust = new Customer();
        cust.setCustomerId(id);
        cust.setName(name);
        cust.setLastNAme(lastname);
        cust.setAddressline1(adress);
        cust.setCity(city);
        customerDAO.add( cust );
    }

In client I do:

Client c = ClientBuilder.newClient();   
WebTarget resource = c.target("http://localhost:8080/WebService/webresources/generic/insertt?id=100&name=test&lastname=test&adress=test&city=test");
//resource.request().post(); // This does not work
kvway
  • 494
  • 4
  • 16

2 Answers2

1
  1. Because you're trying to send POST data @QueryParam will not work because post data will be sent as request body and not as query param (that means not appended in the URL as you did). So you have to change your resource method as follows:

    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Path("insertt")
    public void insert(@FormParam("id") Integer id,
                       @FormParam("name") String name,
                       @FormParam("lastname") String lastname,
                       @FormParam("adress") String adress,
                       @FormParam("city") String city ) {
    
    Customer cust = new Customer();
    cust.setCustomerId(id);
    ...
    customerDAO.add( cust );
    

    }

  2. And change your client as follows:

    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://localhost:8080/WebService/webresources/generic").path("insertt");
    Form form = new Form().param("id", "100").param("name", "test").param("lastname", "test").param("address", "test").param("city", "test");
    Response response = target.request().post(Entity.form(form));
    

This example is just simulating an HTML form submission. If you want to send data as XML or JSON or any other form, you have to look at the JAX-RS documentation. There are lots of resources on the net; here are some example sites:

NOTE: The example is tested with Jersey 2.23 and Wildfly 8.2.1

ujulu
  • 3,289
  • 2
  • 11
  • 14
0

Use the curl command as documented here:

... use curl to post this form with the same data filled in as before, we could do it like:

curl --data "birthyear=1905&press=%20OK%20" http://www.example.com/when.cgi This kind of POST will use the Content-Type application/x-www-form-urlencoded and is the most widely used POST kind.

The data you send to the server MUST already be properly encoded, curl will not do that for you. For example, if you want the data to contain a space, you need to replace that space with %20 etc. Failing to comply with this will most likely cause your data to be received wrongly and messed up.

Recent curl versions can in fact url-encode POST data for you, like this:

curl --data-urlencode "name=I am Daniel" http://www.example.com If you repeat --data several times on the command line, curl will concatenate all the given data pieces - and put a '&' symbol between each data segment.

Guy Bouallet
  • 2,099
  • 11
  • 16