Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send post request to the below post method using postman rest client

I just want to know, how to send JSON object to createTrackInJSON(Track track) method, with @Post annotation through postman rest client. here,how to pass JSON object to createTrackInJSON(Track track) method,with @Post annotation ?

import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response;  import com.mkyong.Track;  @Path("/json/metallica") public class JSONService {      @GET     @Path("/get")     @Produces(MediaType.APPLICATION_JSON)     public Track getTrackInJSON() {          Track track = new Track();         track.setTitle("Enter Sandman");         track.setSinger("Metallica");         System.out.println("inside get method . . .");         return track;      }      @POST     @Path("/post")     @Consumes(MediaType.APPLICATION_JSON)     public Response createTrackInJSON(Track track) {         System.out.println("inside post method . .");         String result = "Track saved : " + track;         return Response.status(201).entity(result).build();      }  }  //Track class is:  public class Track { String title; String singer;  public String getTitle() {     return title; }  public void setTitle(String title) {     this.title = title; }  public String getSinger() {     return singer; }  public void setSinger(String singer) {     this.singer = singer; }  @Override public String toString() {     return "Track [title=" + title + ", singer=" + singer + "]"; }  } 
like image 589
user3962745 Avatar asked Mar 31 '15 09:03

user3962745


People also ask

How do I send a POST request with parameters in Postman?

Right-click selected text, and choose EncodeURIComponent to manually encode a parameter value. To send a path parameter, enter the parameter name into the URL field, after a colon, for example :id . When you enter a path parameter, Postman will populate it in the Params tab, where you can also edit it.

How do I POST on REST client?

To post JSON to a REST API endpoint, you must send an HTTP POST request to the REST API server and provide JSON data in the body of the POST message. You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header.


1 Answers

  1. Open Postman.
  2. Enter URL in the URL bar http://{server:port}/json/metallica/post.
  3. Click Headers button and enter Content-Type as header and application/json in value.
  4. Select POST from the dropdown next to the URL text box.
  5. Select raw from the buttons available below URL text box.
  6. Select JSON from the following dropdown.
  7. In the textarea available below, post your request object:

    {  "title" : "test title",  "singer" : "some singer" } 
  8. Hit Send.

  9. Refer to screenshot below: enter image description here

like image 122
Pramod Karandikar Avatar answered Sep 22 '22 18:09

Pramod Karandikar