4

I'm trying to send a POST request through HttpClient to a Resource but it seems that there is no data to consume..

I've a filter before the call is passed to that resource to verify signature and validate it, but nothing happens after that.. no data is received at the resource end which triggers appropriate exception here is my code to make the request

CloseableHttpResponse response = null;
StringBuilder result = new StringBuilder();
try {
CloseableHttpClient client=HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
HttpPost request = new HttpPost(API_BASE_URL + "/generateToken");
//Set Headers
request.setHeader("Accept", "application/json");
request.setHeader("Content-Type", "application/x-www-form-urlencoded");
request.addHeader("Signature",URLEncoder.encode(generateSignature(),"UTF-8"));
request.addHeader("Expires", String.valueOf(timestamp));
request.addHeader("AccessKey", URLEncoder.encode(accessKey, "UTF-8"));

//Add Paramets
request.setEntity(new UrlEncodedFormEntity(parameters));

//Sends Request
response = client.execute(request);

//Read From Response
BufferedReader reader;
InputStream inputStream = response.getEntity().getContent();
reader = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
while ((inputLine = reader.readLine()) != null) {
result.append(inputLine);
}
EntityUtils.consume(response.getEntity());

I'm sending login and pass as List<NameVaulePair> parameters

My Resource code

@POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public Response getToken(@FormParam("login") String slashtagOrEmail,
            @FormParam("pass") String password) {
        String token = null;
        try {
            token = publisherService.authenticate(slashtagOrEmail, password);
            return Response.ok().entity(token).build();
        } catch (RemoteException | EmailNotFoundException | UsernameNotFoundException | UnknownLoginException | IncorrectPasswordException | AccountNotConfirmedException ex) {
            return Response.status(Response.Status.UNAUTHORIZED).entity(ex.toString()).build();
        }

    }

After I make the request I get UsernameNotFoundException which is basically for empty slashtagOrEmail variable

The JUnit Test for publisherService.authenticate() function gives proper result, I don't see where I'm making mistake(s).

PS:This is my very first question on SO, please help me to frame questions better, Thanks Again

pah
  • 4,700
  • 6
  • 28
  • 37
Kainix
  • 1,186
  • 3
  • 21
  • 33

1 Answers1

0

Judging by your code, you are using a ssl verifier and you might be connecting to http://example.com instead of https://example.com

In this case, its the 302 empty response. Because the server is redirecting port 80(default http port) requests to port 443 (default SSL port)

Check if the response code is 302. HttpClient doesn't automatically redirect to the page. https://stackoverflow.com/a/5170468/1097600

System.out.println("Response Code : " 
            + response.getStatusLine().getStatusCode());

Solution: Change url to https://example.com

Community
  • 1
  • 1
Sorter
  • 9,704
  • 6
  • 64
  • 74
  • `Https` isn't the issue `slashtagOrEmail` and `password` are both becomes `null` after the request is passed through `chain.dofilter()` – Kainix Apr 19 '15 at 13:28