0

For a Order Management Application, i need to design RESTful APIs which works with JSON.

I would prefer my APIs are like these, whereby request/response contains JSON:

Order Search API

API:    /orders/search
            {
                custname: "john",
                orderdate: "10-jun-2013"
            }
     Response:
           {
             orders:[
               {
                 orderid:234234,
                 orderstatus: NEW,
                 customer: "john"
               }
             ]
           }

Order Update API

 API:   /orders/343455        
 Request Body:
   {
            orderstatus: "DELIVERED",
            recepient: "joe"        
   }
 Response:
   {
      status: ERROR,
      message: "Order does not exist"
   }

Question:
1. How can i send JSON in a GET request (as in Order Search API).
2. I am even thinking of making every request a POST request, with JSON request in body, suggesting what the operation is - but then would this still be REST (perhaps 'RESTful Web-Service' or 'REST like Service' )?
3. I think its important for me to send JSON in most requests, that way my APIs implementations do not undergo much changes, just becz i added removed attribs to JSON message.
4. Are there any examples of how other people have done it, especially returning error messages.

Any thoughts?

4

1 回答 1

0
  1. 如何在 GET 请求中发送 JSON(如在 Order Search API 中)。

据我所知,HTTP 1.1并没有明确禁止 GET 方法的请求正文。

但是,是否推荐这样做的意见不同。

然而,GET 请求应该(必须)是幂等的——尤其是在 REST 上下文中,并且为了允许缓存。

另请参阅带有请求正文和许多其他内容的 SO HTTP GET上的这个问题。

如果您无法在 GET 方法中指定请求正文,无论出于何种原因,您都需要在 URL 查询中包含参数 - 正确编码(这很痛苦)。

  1. 我什至在考虑让每个请求都成为 POST 请求

我不会那样做的。Web 服务框架期望使用相应的 HTTP 方法执行某些类型的请求。它与 REST 相悖,例如,实际上是 GET 的 POST 请求不会被缓存。这也违反了 HTTP。

于 2013-09-25T10:36:46.490 回答