37917

Jersey Client resulting in 415 Unsupported media type

Question:

The obvious reason for this would be not providing proper content type. But I am providing. Still i am getting Unsupported Media Type. Not sure why. Any help greatly appreciated.

Client c = Client.create(); WebResource resource = c.resource(HOST+"/test"); Gson gson = new Gson(); Test test = new Test(); test.setTestName("TEST AUTOMATION"); resource.header("Content-Type", "Application/json"); String testStr = gson.toJson(test); System.out.println("Request Str: "+testStr); ClientResponse response = resource.post(ClientResponse.class, testStr); System.out.println("POST response : "+response); POST response : POST http://host:8888/test returned a response status of 415 Unsupported `enter code here`Media Type

Answer1:

This is how i solved it. Its really weird. Until i combine the statements as below, it didn't work. From the above program that i wrote, combine the header statement and post statement as below. Also don't forget to put charset=UTF-8.

ClientResponse response = resource.header("Content-Type", "application/json;charset=UTF-8").post(ClientResponse.class, testStr);

Recommend

  • Changing word color to red based on list of words
  • Runtime error when attempting to delete a worksheet from a newly created workbook - Outlook VBA
  • How to fill a ComboBox with values from a list, and bind the selected item to a string in another Vi
  • Program type already present: com.squareup.okhttp.Address
  • How to call a postgresql function from the JOOQ models in a select statement?
  • Java 8 LocalDate in Grails 3.2 JSON view
  • how to do a GET request using retrofit2?
  • kotlin convert json array to model list using GSON
  • Using GSON to parse Json into a JAVA Object where the Json Elements may vary
  • How to parse nested JSON with GSON
  • fromJson raises unparseable date exception
  • Android NFC read Tags issue. Activity starts each time on data received
  • openssl handshake failed
  • How to resolve docker host names (/etc/hosts) in containers
  • Switch to popup in python using selenium
  • Who propagate bugfixes across branches (corporate development)?
  • Web.config system.webserver errors
  • ADO and msqli connections very slow
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • Problems compiling files using JOGL
  • HTTP/2 streams vs HTTP/1.1 connections
  • Play WS (2.2.1): post/put large request
  • Python urlparse: small issue
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • Different response to non-authenticated users and AJAX calls
  • What is Eclipse's Declaration View used for?
  • Counter field in MS Access, how to generate?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • sending/ receiving email in Java
  • JSON with duplicate key names losing information when parsed
  • How to set my toolbar fixed while scrolling android
  • Jquery - Jquery Wysiwyg return html as a string
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • need help with bizarre java.net.HttpURLConnection behavior
  • apache spark aggregate function using min value
  • Is it possible to post an object from jquery to bottle.py?
  • Sorting a 2D array using the second column C++