Reading HTTP Headers

Hi, I'm having problems. I am trying to learn soothing services. I created a web service using jax-rs which is shown below

@Path("/users") public class Welcome { @POST @Consumes("text/xml") @Produces("text/xml") public Response welcome(String incomingXML){ return Response.status(200).entity("timestamp : " + incomingXML).build(); } } 

I use the following test client to test the service

 public class TestService { public static void main(String args[]) throws ParserConfigurationException, SAXException, IOException { ClientConfig config = new DefaultClientConfig(); Client client=Client.create(config); WebResource service=client.resource(getBaseURI()); String urlString = "http://localhost:8080/JaXRSDemo/rest/users"; URL url = new URL( urlString ); HttpURLConnection con = (HttpURLConnection) url.openConnection(); // set up url connection to get retrieve information back con.setRequestMethod( "POST" ); con.setDoInput( true ); // stuff the Authorization request header byte[] encodedPassword = ( userName + ":" + password ).getBytes(); con.setRequestProperty( "Authorization",encodedPassword.toString() ); Customer customer=new Customer(); customer.setName("noobstre"); customer.setPin(123455); ClientResponse response=service.path("rest").path("users").type(MediaType.APPLICATION_XML).post(ClientResponse.class,customer); System.out.println(" response " + response.getEntity(String.class)); } private static URI getBaseURI() { return UriBuilder.fromUri("http://localhost:8080/JaXRSDemo").build(); } } 

I want to use the password in the header on the server side and search using the database. The problem I ran into is how to read the headers on the server.

+6
source share
2 answers

I decided to use it with the Jersey Client

// client

  ClientConfig config = new DefaultClientConfig(); Client client = Client.create(config); final String userName = "admin"; final String password = "admin"; String cred = userName + ":" + password; WebResource service = client.resource(getBaseURI()); Customer customer = new Customer(); customer.setName("noob"); customer.setPin(123455); ClientResponse response = service.path("rest").path("users") .accept(MediaType.APPLICATION_XML) .header("Authorization", cred) .post(ClientResponse.class, customer); System.out.println(" response " + response.getEntity(String.class)); 

Server side

 @Path("/users") public class Welcome { @POST @Consumes(MediaType.APPLICATION_XML) @Produces(MediaType.APPLICATION_XML) public Response welcome(String incomingXML, @Context HttpHeaders headers) { String s = headers.getRequestHeaders().getFirst("authorization"); return Response.status(200).entity("timestamp : " + incomingXML + s) .build(); } 

}

+7
source

I am not very familiar with Jax-RS, but you can use the following methods to get the header information you are looking for:

1.) Use @HeaderParam

 /**Server side******/ @Path("/users") public class Welcome { @POST @Consumes("text/xml") @Produces("text/xml") public Response welcome(String incomingXML, @HeaderParam("Authorization") String authString) { //Use authString here return Response.status(200).entity("timestamp : " + incomingXML).build(); } } 

2.) Use @Context

 /**Server side******/ @Path("/users") public class Welcome { @POST @Consumes("text/xml") @Produces("text/xml") public Response welcome(String incomingXML, @Context HttpHeaders headers) { //Get Authorization Header String authString = headers.getRequestHeader("Authorization").get(0); return Response.status(200).entity("timestamp : " + incomingXML).build(); } } 

Hope this helps!

+10
source

Source: https://habr.com/ru/post/910556/


All Articles