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