Getting Error for jersey client request headers?

504 Views Asked by At

I need to set these headers for a REST call via jersey client. clickatell message send rest call

curl -i \
            -X POST \
            -H "X-Version: 1" \
            -H "Content-Type: application/json" \
            -H "Authorization: Bearer Your Authorization Token" \
            -H "Accept: application/json" \

My code is:

Client client = Client.create();
WebResource webResource = client.resource("https://api.clickatell.com/rest/message");

ClientResponse response = webResource
                            .header("Authorization", "Bearer clickATellAuthKey")    
                            .header("X-Version", "1")
                            .header("Content-Type", "application/json")
                            .header("Accept", "application/json")
                            .post(ClientResponse.class, input); 

I am getting this error:

{"error":{"code":"001","description":"Authentication failed","documentation":"http://www.clickatell.com/help/apidocs/error/001.htm"}}

the document says the auth header is not specified. The request is working fine in Postman ( chrome restful client extension) with same headers

Need help.

2

There are 2 best solutions below

0
On BEST ANSWER

Thanks @whatever_sa there are some improvements required as well in code and also there was an issue with auth key your answer at least make me check the auth key once more. here is the working code

    ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)                   
                        .header(MessageServices.API_AUTH_HEADER, MessageServices.AUTH_KEY)
                        .header(MessageServices.API_VERSION_HEADER, MessageServices.API_VERSION)                            
                        .post(ClientResponse.class, input);
0
On

1) Your headers seem to be going through. If they were not, you would get an error about not setting a version header.

2) The 001 error means your auth token was either not specified, or is incorrect.

3) I suggest that you copy and paste your entire auth token and try again. Watch out for _ or . characters as they are part of the token.