0
\$\begingroup\$

I have this class in java, I'm using Jersey.

 @Path("/bandas")
 public class BandaResource {

static private Map<Integer, Banda> bandasMap=someSource();

@GET    
@Produces({ "application/json" })
public List<Banda> getBandas()
 {
    return new ArrayList<Banda>(bandasMap.values());
 }



@POST
@Consumes("text/xml")
@Produces("text/plain")
          public String adicionaBanda(Banda banda) {
           banda.setId(bandasMap.size() + 1);
           bandasMap.put(banda.getId(), banda);
           return banda.getNome() + " adicionado.";
    }
}

In Unity when I request all the information I get an answer from server:

 URL url = new URL ( "http://localhost:8080/restful-exemplo/rest/bandas" );
 WWW www = new WWW(url);

Debug.Log (www.data) returns:

{"banda":[{"anoDeFormacao":"1968","id":"1","nome":"Led Zeppelin"},{"anoDeFormacao":"1973","id":"2","nome":"AC/DC"}]}

But I want to send, I don't have any idea how but I tried:

 string JSON=" {\"nome\":\"Teste\", \"anoDeFormacao\":\"1988\",\"id\":\"4\"}";

    Hashtable postHeader = new Hashtable();     
    postHeader.Add("Content-Type", "text/json");


    UTF8Encoding encoding = new System.Text.UTF8Encoding();
    WWW request = new WWW(url, encoding.GetBytes(JSON.ToCharArray()), HashtableToDictionary<string, string>(postHeader));

Well, it returns a error (WWW Error: 415 Unsupported Media Type ), is the idea correct?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Your Java method is declared with @Consumes("text/xml"). That means it expects the MIME-type text/xml and XML-encoded data.

But on the Unity-side you are using a header to set the content-type to text/json and send JSON-encoded data. The result is a 451 Unsupported Media Type HTTP error, just like the documentation says.

Make sure the Unity-side sets the content-type header to the same MIME-type the server method consumes. When you want to use JSON, then that would be application/json (text/json is incorrect, but might still work). If you would like to use text/xml instead, you also need to format your data as XML on the Unity-side.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .