1

I'm sending a 'POST' request containing a JSON object converted to JSON from my Knockout observable via ko.toJSON method. I send it using Amplify.

This is my Amplify setting:

 amplify.request.define('addContract', 'ajax', {
                url: '/api/contractmanager/contracts/create',
                dataType: 'json',
                type: 'POST'
            });

And this is the method in my dataservise to add data:

addContract = function (callbacks, data) {
        return amplify.request({
            resourceId: 'addContract',
            data: data,
            success: callbacks.success,
            error: callbacks.error
        });
    };

Here is how I actually send the request:

contracts.addData = function (contractModel, callbacks) {

        var contractModelJson = ko.toJSON(contractModel);

        return $.Deferred(function (def) {
            dataservice.contract.addContract({
                success: function (dto) {
                    if (!dto) {
                        logger.error('Error saving!');
                        if (callbacks && callbacks.error) { callbacks.error(); }
                        def.reject();
                        return;
                    }
                                        },
                error: function (response) {
                    logger.error('Error saving!');
                    if (callbacks && callbacks.error) { callbacks.error(); }
                    def.reject(response);
                    return;
                }
            }, contractModelJson);
        }).promise();
    };

For the back-end I am using a RESTful web service with Jersey.

The problem is that as soon as I send the request I get a "HTTP Status 415 - Unsupported Media Type" and my server GlassFish says: "The server refused this request because the request entity is in a format not supported by the requested resource for the requested method (Unsupported Media Type)."

Do I need to set anything particular for this to work? Am I missing something?

Pejman
  • 3,784
  • 4
  • 24
  • 33

1 Answers1

1

You need to ensure that the content type sent matches the content type of the @Consumes annotation on the Jersey resource. In your particular case the Jersey resource needs to look something like:

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;

...

@PATH("create")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Item create(final Item item) {
  // Create here
}

and you need to ensure that your data is sent using the content type 'application/json'.