1

我可以直接调用 API URI 执行创建/删除操作吗?就像是

http://www.somedomain.com/api/entry/?action=create&book_title=something&year=1986&author=someone

而不是在卷曲中传递标题?

我可以用 CURL 做到这一点:

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"name": "me", "passwd": "123456"}' http://www.somedomain.com/api/entry/

但我希望通过在浏览器中请求 url 而不是使用 curl 来执行此操作。这可能在美味派中吗?

4

2 回答 2

1

您可以覆盖您的dispatch方法Resources

from tastypie import resources

class MyResource(resources.ModelResource):
    class Meta:
        # TODO stuff here

    def dispatch(self, request_type, request, **kwargs):
        action = request.GET.get("action")
        if action in ["POST", "PUT", "DELETE", "PATCH"] and request.method == "GET":
            request.method = action
            request.POST = request.GET
        return super(MyResource, self).dispatch(request_type, request, **kwargs)

然后,您可以使用以下网址直接调用您的 api GET

http://www.somedomain.com/api/entry/?action=POST&book_title=something&year=1986&author=someone
于 2013-06-30T09:35:17.843 回答
0

GET puts arguments in the URL, POST requests should put their data in the message body

http://en.wikipedia.org/wiki/POST_%28HTTP%29

于 2013-07-03T11:56:15.470 回答