I am trying to send a POST request via a WebResource instance using a Jersey Client. But no matter how i specify the request headers or types, it will give me a 415 Unsupported Media Type
error. Below is my code
String SERVICE_URL = "http://hostIpAddress:8080/JiraUpdate/rest/createin/jira/createticket?"; // hostIpaddress is our server ip
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
System.out.println("SERVICE_URL=" + SERVICE_URL);
WebResource webResource = client.resource(UriBuilder.fromUri(SERVICE_URL).build());
String input = "{\"fields\":{\"project\":{\"key\":\"Invoicing\"},\"summary\":\"REST Test\",\"description\": \"Creating of an issue using project keys and issue type names using the REST API\",\"issuetype\":{\"name\":\"Bug\"}}}";
ClientResponse response = webResource.header("Content-Type", "application/json;charset=UTF-8")
.type(MediaType.TEXT_PLAIN_TYPE).post(ClientResponse.class, input);
I have tried to set the type to MediaType.APPLICATION_FORM_URLENCODED_TYPE
as well as MediaType.APPLICATION_JSON
but it doesnt change the response. I am running this code as a standalone executable JAR in a linux environment. Any help is appreciated.
Try the below, based upon Vijay-Bhushan's example in this SO article.
Note, he mentions that UTF-8 was not needed.
String SERVICE_URL = "http://hostIpAddress:8080/JiraUpdate/rest/createin/jira/createticket?"; // hostIpaddress is our server ip
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
System.out.println("SERVICE_URL=" + SERVICE_URL);
WebResource webResource = client.resource(UriBuilder.fromUri(SERVICE_URL).build());
String input = "{\"fields\":{\"project\":{\"key\":\"Invoicing\"},\"summary\":\"REST Test\",\"description\": \"Creating of an issue using project keys and issue type names using the REST API\",\"issuetype\":{\"name\":\"Bug\"}}}";
ClientResponse response = webResource.header("Content-Type","application/json;charset=UTF-8").post(ClientResponse.class,input);
The Jersey Client API documentation has some great examples.