I am calling a post request using Jersey rest client which contains no request body but contains authorization header. I am always getting 400 error. I tried from postman i got 200 response. Here is my code
ClientResponse response = null;
Client cliente=Client.create();
cliente.addFilter(new LoggingFilter(System.out));
WebResource webResource=cliente.resource("https://URL/token");
Builder builder = webResource.accept("application/json");
builder.type(MediaType.APPLICATION_JSON);
builder.header("Authorization", "Basic YbjjkwliOTQtNjRiYy00NWE5LWFhMzQtMTFhNDkyZZjNTVlOjZjYjk2OTMwNGE5YTQ3OTlhODVjZTM5MDFiMDEyMTI2";
builder.post(ClientResponse.class,Entity.json(""));
Don't use the way you are trying right now. Use HttpAuthenticationFeature from Jersey like this
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("username", "password");
final Client client = ClientBuilder.newClient();
client.register(feature);
If you are getting 200 success respone in postman and failure response in your application then you need to check your api with another api tester online tool. I had the same issue i put / at the last of end point.
Related
I'm working on a project which requires to call GitHub APIs several times and I reached the limit of 60.
I read that with authentication you get 5000 as limit but I can't understand how I can authenticate my requests in my java program. I got my authentication token on Github and this is the way I'm building the request in java:
// create client
HttpClient client = HttpClient.newHttpClient();
// create request
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.github.com/repos/:owner/:repo/commits"))
.build();
what should I add to the request to authenticate it?
I tried adding the header authToken:myToken but it didn't work.
Solved:
Once I got the token on my GitHub profile > Settings > Developer Settings > Personal Access Tokens, I added the header `"Authorization: Bearer "myToken" " to the http request so the request becomes:
// create client
HttpClient client = HttpClient.newHttpClient();
// create request
HttpRequest request = HttpRequest.newBuilder().header("Authorization","Bearer <myToken>")
.uri(URI.create("https://api.github.com/repos/:owner/:repo/commits"))
.build();
You need to add Http request header Authorization to your request and the header should contain your token. So if your code is written on Java 11 or higher as it appears to be than you need to change your code to:
// create client
HttpClient client = HttpClient.newHttpClient();
// create request
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.github.com/repos/:owner/:repo/commits"))
.header("Authorization", "your-tocken")
.build();
I'm diagnosing an issue and would like to know the simplest way to call a SOAP web service. I'd like to setup a simple junit (integration) test that will hit a SOAP service. I have a SoapUI request working as intended, I'd like to take the URL and soap envelope XML request as a String from that, call it from a Java class and have the response as a String. I'm trying to avoid generating all of the objects/clients/etc usually involved with working with a SOAP service. I'm just looking for a quick and dirty way to accomplish this.
I found that the following meets my needs:
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(SERVICE_URL)
.put(RequestBody.create(MediaType.parse("application/octet-stream"), REQUEST_BODY))
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request).execute();
String responseBody = response.body().string();
I have a question on how to fetch response body in Jersey client when server returns some sample text with status code 401. Sample service is setup as follows:
#GET
#Path("test401withcontent")
public Response get401TestWithContent()
{
return Response.status(401).entity("return some text").build();
}
On the client side (using Jersey 1.17) ClientResponse.getEntity prints null.
Noticed that content-length of headers has the right number (16 in this case.)
Is there a different way to get response when return code is 401?
Have deployed you method to my test web site and used below client got currect response.
Client client = ClientBuilder.newClient();
//System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
Response response = client.target(
"http://jerseyexample-ravikant.rhcloud.com/rest/jws/test401withcontent").
request().get(); System.out.println(response.readEntity(String.class));
I am using the following code to consume a rest post service
Client client = ClientBuilder.newClient();
WebTarget target = client.target("wrong url");
Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON);
Response response = builder.post(Entity.entity(param, MediaType.APPLICATION_JSON), Response.class);
As expected I am getting error. and status code 400 Bad Request. But i am not getting the error message. when i run response.getStatusInfo() i get bad request but my server sends additional info.
when i call this using postman i get error info in the Body window.
So how do I get this error body info from the response object?
or any other way???
You get the response body as usual with Response#readEntity:
Read the message entity input stream as an instance of specified Java type using a MessageBodyReader that supports mapping the message entity stream onto the requested type.
Example:
Client client = ClientBuilder.newClient();
WebTarget target = client.target("wrong url");
Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON);
Response response = builder.post(Entity.entity(param, MediaType.APPLICATION_JSON), Response.class);
String body = response.readEntity(String.class);
With Response#getStatusInfo you get only HTTP status, class and reason phrase.
I am new to Jersey and JAX-RS, but I believe this question is equivalent to Handling custom error response in JAX-RS 2.0 client library, which has much better answers.
I'm new to OAuth and trying to send a https GET request to retrieve something. Earlier I was using POSTMAN to test that and I was able to execute the GET request with OAUth 1.0 header authorization. The header authorization looks something as
Authorization: OAuth oauth_consumer_key="xxxxxxxxxxxxxxxxxxx" ,oauth_signature_method="HMAC-SHA1" ,oauth_timestamp="1409861973" ,oauth_nonce="x1409861973681" ,oauth_version="1.0" ,oauth_signature="M+Dq62XboEd3+t6VDIcLy86zlQg="
The query looks something as
https://secure.api.abc.net/DataService/data/ServiceAccount?schema=1.0&form=json&byBillingAccountId={EQUALS,yyyyy}
Note that I'm able to execute this fine from POSTMAN.
Now, I need to code that in java and I'm able to generate the oauth signature fine, but I'm wondering how do I set the authorization header after that in a https request???
Please advise as I'm new to oauth and want to learn.
I guess if your doing in Java, you will need to use the Apache HttpClient or something similar to make that request to the server and set the OAuth header to the request.
Code sample using the Apache HttpClient below.
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
// add request header
request.addHeader("OAuth", oauthHeaderString);
HttpResponse response = client.execute(request);