API requires POST arguments in a query string?

2019-02-06 00:23发布

I'm playing with the Twitter API and noticed something funny- For updates, they require POST methods but expect the arguments in the query string. (See for example, the status/update call in their developer console here.)

Obviously this is technically possible, but why would anyone do it like that? Don't POST arguments belong in the body?

3条回答
爷、活的狠高调
2楼-- · 2019-02-06 00:58

In REST architecture, GET and POST are just the verbs, which tells either to retrieve or create/update the resource. URI defines the identification of resource.

Example:

POST /student?name=Tom&age=12 >> It will create a new student with name Tom and age 12.
POST /student/10?name=Tom&age=12 >> It will update student with id 20 with name Tom and age 12.

There is no rule that the data should be binded to body payload or URI. This is different from WEB 1.0 concepts where HTML form data is sent in POST.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-02-06 01:05

If the arguments for WEB API are in the body or query depends on the Content-Type header you send in the POST.

If it forinstance is Content-Type: application/json; charset=UTF-8 then the arguments are expected in the body as json. If it is Content-Type: application/x-www-form-urlencoded; charset=UTF-8 then the arguments are expected in the query string

查看更多
成全新的幸福
4楼-- · 2019-02-06 01:07

Either option is just as valid. My favourite example for using parameters in the URL for a POST is an application that sets waypoints on a map. e.g.

     POST /map/route/45/waypoints?lat=35&long=74

In this case, the parameters make more sense in the URI as identifiers of a location, than just parameters being passed in the body to generic resource.

查看更多
登录 后发表回答