简体   繁体   中英

Rest DELETE Bad Request

Can u explain me why DELETE method (store.remove() in Edit.js) throws 400 Bad request. Other method works well. In header request url seems to be ok " http://localhost:8080/Diary/rest/notes/22?_dc=1461837327580 ".

I know that problem is in payload of DELETE method, store.remove() includes ID as payload. How can i disable that and send DELETE method without body, because ID is already in URL

Rest Service

@Path("/notes")
public class NoteRestService {
@Context
private UriInfo uriInfo;
@Context
private HttpServletRequest request;



private NoteDaoImpl noteDao = new NoteDaoImpl();
@GET
@Produces("application/json")
public String getNotes(){
    String login = request.getSession(true).getAttribute("login").toString();
    List<Note> notes = noteDao.getUserNotes(login);
    return new Gson().toJson(notes);
}

@POST
@Consumes("application/json")
public Response postNote(Note note){
    String login = request.getSession(true).getAttribute("login").toString();
    note.setUser(login);
    noteDao.persist(note);
    URI noteUri = uriInfo.getAbsolutePathBuilder().path(Long.toString(note.getId())).build();
    return Response.created(noteUri).build();
}

@PUT
@Path("{id}")
@Consumes("application/json")
public Response updateNote(@PathParam("id") String id,Note note){
    String login = request.getSession(true).getAttribute("login").toString();
    Note editNote = noteDao.getNote(Long.parseLong(id));
    note.setCreated(editNote.getCreated());
    note.setUser(login);
    noteDao.update(note);
    return Response.ok().build();
}

@DELETE
@Path("{id}")
public Response deleteNote(@PathParam("id") String id){
    Note note = noteDao.getNote(Long.valueOf(id));
    if (note==null){
        throw new NotFoundException();
    }
    noteDao.delete(Long.parseLong(id));
    return Response.noContent().build();
}
}

EditController.js

Ext.define('MVC.controller.Edit', {
extend: 'Ext.app.Controller',


init: function () {
    this.control({
        'editForm > button#SaveRecord': {
            click: this.onSaveButtonClick
        },
        'editForm > button#DeleteButton': {
            click: this.onDeleteButtonClick
        }
    });
},

onSaveButtonClick: function (btn) {
    //get reference to the form
    var detailView = btn.up('editForm');

    //get the form inputs
    var data = detailView.getValues();

    //see if the record exists
    var store = Ext.getStore('TestStore');
    console.log(data.id);
    var record = store.getById(data.id);

    if (!record) {
        record = Ext.create('MVC.model.Note', {
            title: data.title,
            created: new Date(),
            updated: new Date(),
            text: data.text
        });
        Ext.MessageBox.alert('Created', data.title);

        store.insert(0, record);
        store.sync();
        return;
    }

    record.set(data);

    store.sync();
    //manually update the record
    detailView.updateRecord();
},

onDeleteButtonClick: function (btn) {

    //get reference to the form
    var detailView = btn.up('editForm');

    //get the form inputs
    var data = detailView.getValues();

    var store = Ext.getStore('TestStore');
    var record = store.getById(data.id);
    store.remove(record);
    store.sync();
}
});

UPD: Store

Ext.define('MVC.store.TestStore', {
extend: 'Ext.data.Store',

requires: [
    'MVC.model.Note'
],

storeId: 'TestStore',
model: 'MVC.model.Note',
autoLoad: false,
proxy: {
    type: 'rest',
    url: 'rest/notes',
    actionMethods: {
        create: 'POST',
        read: 'GET',
        update: 'PUT',
        destroy:' DELETE'
    },
    reader: {
        type: 'json',
        rootProperty: 'data'
    },
    writer: {
        type: 'json',
        writeAllFields: true
    }
}
});

You can't have a HttpMethod.DELETE with a body.

This is not explicitly stated in the RFC, but some Proxy servers will reject the body if you have one in a delete method. Spring lowers the standard and will reject your query with a Bad Request .

Remove the body as well as the answer to fix your issue.

Check this for more information: Is an entity body allowed for an HTTP DELETE request?

If TestStore is the store you're using, I'd guess that your problem is here:

 actionMethods: { create: 'POST', read: 'GET', update: 'PUT', destroy: 'GET' }, 

I don't recognize the @DELETE annotation, so I'm not 100% sure but if your controller is expecting DELETE, and you're sending GET, that could explain the 400 error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM