How to pass object to RESTful Service with GET req

2020-02-13 06:40发布

I have seen some posts in stackoverflow saying "sending list of items in the GET Method, is NOT allowed. It has to be accomplished via POST method only"

My code looks like

    [OperationContract]
    [WebGet(UriTemplate = "Employee/{emp}",RequestFormat=WebMessageFormat.Json)]
    Employee GetEmpDetails(string emp);

and my input json object will be "{'id':1,'name':'test',....} Is there any alternative way of achieving this issue.

Thanks

标签: wcf rest
3条回答
爷、活的狠高调
2楼-- · 2020-02-13 06:59

If you make your service RESTful you will most probably use HTTP PUT for Add method and HTTP POST for Update method. It is absolutely ok to pass object to these methods because objet will be part of HTTP request's body, not part of URI. URI is important for HTTP GET requests. HTTP GET requests should be only for data retrieval not for data modification.

查看更多
叛逆
3楼-- · 2020-02-13 07:03

You are mixing up HTTP GET/POST/... requests and REST GET/POST/PUT/DELETE/... When you wanna request something RESTfully - you do a GET request. In your case I think it should look like

employee/{id}

or

employee/{name}

Please also note that usage of lowercase in the URI is preferable. If you need multiple GET criteria, I think it could look like:

employee/id/{id}/name/{name}
查看更多
趁早两清
4楼-- · 2020-02-13 07:06

It is possible to send list of items with GET, it's just that out of the box only primitive values are supported. String values work just fine, but if you want to pass a complex object, you need to create a custom QueryStringConverter. The post at http://blogs.msdn.com/b/carlosfigueira/archive/2011/08/09/wcf-extensibility-querystringconverter.aspx explains how this can be done.

查看更多
登录 后发表回答