Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read http post headers

Tags:

http

jaxb

jax-rs

Hi I am having trouble I am trying to learn restful 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 at the server side and do a lookup with the database.The problem I am facing is how do I read the headers at the server.

like image 206
luckysing_noobster Avatar asked Mar 13 '12 01:03

luckysing_noobster


People also ask

How do I read HTTP request headers?

Reading headers is very straightforward; just call the getHeader method of the HttpServletRequest , which returns a String if the header was supplied on this request, null otherwise. However, there are a couple of headers that are so commonly used that they have special access methods.

What is header in HTTP POST?

An HTTP header is a field of an HTTP request or response that passes additional context and metadata about the request or response. For example, a request message can use headers to indicate it's preferred media formats, while a response can use header to indicate the media format of the returned body.

How do you get response header messages?

Most actionable response headers are generated by the Web server itself. These include instructions for the client to cache the content (or not), content language, and the HTTTP request status code among others.


2 Answers

I'm not very familiar with Jax-RS, but you might use the following methods to get the header information you're 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!

like image 157
mrdc Avatar answered Nov 15 '22 11:11

mrdc


I solved it using Jersey Client

//clientside

    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));

At the 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();
}

}

like image 22
luckysing_noobster Avatar answered Nov 15 '22 09:11

luckysing_noobster