REST API最佳实践:查询字符串与请求正文中的args [英] REST API Best practices: args in query string vs in request body

查看:373
本文介绍了REST API最佳实践:查询字符串与请求正文中的args的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

REST API可以在多个地方包含参数:

A REST API can have arguments in several places:


  1. 在请求正文中 - 作为json正文或其他MIME类型

  2. 查询中字符串 - 例如 / api / resource?p1 = v1& p2 = v2

  3. 作为网址路径的一部分 - 例如 / api / resource / v1 / v2

  1. In the request body - As part of a json body, or other MIME type
  2. In the query string - e.g. /api/resource?p1=v1&p2=v2
  3. As part of the URL-path - e.g. /api/resource/v1/v2

什么是选择1到2之间的最佳做法和考虑因素?

2 vs 3被涵盖这里

推荐答案


选择1
和2以上的最佳做法和注意事项是什么?

What are the best practices and considerations of choosing between 1 and 2 above?

通常内容正文用于要上传/下载到服务器/从服务器下载的数据,查询参数用于指定所请求的确切数据。例如,当您上传文件时,在主体中指定名称,mime类型等,但是当您获取文件列表时,可以使用查询参数按文件的某些属性过滤列表。通常,查询参数是查询的属性而不是数据。

Usually the content body is used for the data that is to be uploaded/downloaded to/from the server and the query parameters are used to specify the exact data requested. For example when you upload a file you specify the name, mime type, etc. in the body but when you fetch list of files you can use the query parameters to filter the list by some property of the files. In general, the query parameters are property of the query not the data.

当然这不是一个严格的规则 - 您可以以任何您认为更合适的方式实现它/为你工作。

Of course this is not a strict rule - you can implement it in whatever way you find more appropriate/working for you.

您可能还想查看维基百科有关查询字符串的文章,尤其是前两段。

You might also want to check the wikipedia article about query string, especially the first two paragraphs.

这篇关于REST API最佳实践:查询字符串与请求正文中的args的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆