在我的API服务器中,我定义了此路由:
POST /categories
Run Code Online (Sandbox Code Playgroud)
要创建一个类别,您可以:
POST /categories {"name": "Books"}
Run Code Online (Sandbox Code Playgroud)
我想如果你想创建多个类别,那么你可以这样做:
POST /categories [{"name": "Books"}, {"name": "Games"}]
Run Code Online (Sandbox Code Playgroud)
我只是想确认这是Restful HTTP API的一个好习惯.
或者应该有一个
POST /bulk
Run Code Online (Sandbox Code Playgroud)
允许他们一次做任何操作(创建,阅读,更新和删除)?
首先,我想知道的是我正在做的是正确的方法.
我有一个场景,我将收到一个json请求,我必须更新数据库,一旦数据库更新,我必须回复json确认.
到目前为止我所做的是创建类扩展应用程序如下:
@Override
public Restlet createRoot() {
// Create a router Restlet that routes each call to a
// new instance of ScanRequestResource.
Router router = new Router(getContext());
// Defines only one route
router.attach("/request", RequestResource.class);
return router;
}
Run Code Online (Sandbox Code Playgroud)
我的资源类是扩展ServerResource,我的资源类中有以下方法
@Post("json")
public Representation post() throws ResourceException {
try {
Representation entity = getRequestEntity();
JsonRepresentation represent = new JsonRepresentation(entity);
JSONObject jsonobject = represent.toJsonObject();
JSONObject json = jsonobject.getJSONObject("request");
getResponse().setStatus(Status.SUCCESS_ACCEPTED);
StringBuffer sb = new StringBuffer();
ScanRequestAck ack = new ScanRequestAck();
ack.statusURL = "http://localhost:8080/status/2713"; …Run Code Online (Sandbox Code Playgroud) 我发送一个创建新用户的POST,这是有效的.
我的问题是我如何回到例如创建用户的pk到ajax响应?
$.ajax({
url: 'http://localhost:8080/api/v1/create/user/',
type: 'POST',
contentType: 'application/json',
data: '{"uuid": "12345"}',
dataType: 'json',
processData: false,
success: function (r) {
console.log(r)
},
});
def obj_create(self, bundle, request=None, **kwargs):
try:
user = User.objects.create_user(bundle.data['uuid'],'1')
user.save()
except:
pass
return bundle
Run Code Online (Sandbox Code Playgroud)