在路径或正文中发布参数 [英] Post parameter in path or in body

查看:137
本文介绍了在路径或正文中发布参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建Rest API,但我对URL结构感到困惑。我必须在Post请求中向服务器发送一个参数。我应该通过路径变量还是在请求体中发送它?什么是最佳做法?

I am creating Rest API but I am confused in URL structure. I have to send just one parameter to server in my Post request. should I send it through path variable or in request body? what are the best practices?

示例当前网址:

api/v1/users/{id}/name/{name}

name 是我想发送给服务器以改变状态的变量

name is the variable I want to send to server for to change state

谢谢

推荐答案

URL通常标识您要更新的资源。

URL usually identifies resource you want to update.

因此数据应该放在请求正文中

So the data should go inside in request body

要更新用户名,您可以将其发送到服务器:

To update user name you may send this to server:

POST api/v1/users/{id} HTTP/1.1
Content-Type: application/x-www-form-urlencoded

name=string

这篇关于在路径或正文中发布参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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