limenitis

Java http post

Jan 11th, 2022
1,022
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. // Maven : Add these dependecies to your pom.xml (java6+)
  2. // <dependency>
  3. //     <groupId>org.glassfish.jersey.core</groupId>
  4. //     <artifactId>jersey-client</artifactId>
  5. //     <version>2.8</version>
  6. // </dependency>
  7. // <dependency>
  8. //     <groupId>org.glassfish.jersey.media</groupId>
  9. //     <artifactId>jersey-media-json-jackson</artifactId>
  10. //     <version>2.8</version>
  11. // </dependency>
  12.  
  13. import javax.ws.rs.client.Client;
  14. import javax.ws.rs.client.ClientBuilder;
  15. import javax.ws.rs.client.Entity;
  16. import javax.ws.rs.core.Response;
  17. import javax.ws.rs.core.MediaType;
  18.  
  19. Client client = ClientBuilder.newClient();
  20. Entity payload = Entity.json("{  \"question\": \"Favourite programming language?\",  \"choices\": [    \"Swift\",    \"Python\",    \"Objective-C\",    \"Ruby\"  ]}");
  21. Response response = client.target("https://polls.apiblueprint.org/questions
  22. ")
  23.   .request(MediaType.APPLICATION_JSON_TYPE)
  24.   .post(payload);
  25.  
  26. System.out.println("status: " + response.getStatus());
  27. System.out.println("headers: " + response.getHeaders());
  28. System.out.println("body:" + response.readEntity(String.class));
Advertisement
Add Comment
Please, Sign In to add comment