简体   繁体   English

如何将标志添加到现有的POST API

[英]How to add a flag to existing POST api

I have a REST api built for creating orders. 我有一个用于创建订单的REST API。 The behavior is such that the person who creates an order received an email back saying "You created an order XXX". 这样的行为使得创建订单的人收到一封回信,上面写着“您创建了订单XXX”。 This email is triggered all the time. 该电子邮件一直被触发。

The api appears like this api看起来像这样

http://api.mytestevnt.com/ordering/orders - POST with request body as the order entity json. http://api.mytestevnt.com/ordering/orders-以请求正文作为订单实体json的POST。

Now i want to give a feature to the api caller to indicate if the email notification is necessary or not. 现在,我想给api调用程序一个功能,以指示是否需要电子邮件通知。 What's the best way to do this? 最好的方法是什么?

I think it depends on whether email notification is data or metadata. 我认为这取决于电子邮件通知是数据还是元数据。 If it's part of the order, then definitely add it to the request body. 如果它是订单的一部分,则一定要将其添加到请求正文中。 If it's metadata, you have two choices. 如果是元数据,则有两个选择。 If you think there will be lots of metadata, you can either edit the order to have a metadata section or you can POST the metadata separately. 如果您认为元数据很多,则可以编辑订单以包含元数据部分,也可以单独发布元数据。 If there will only be a limited amount of metadata, I would suggest using a query parameter. 如果元数据数量有限,我建议使用查询参数。

You should avoid using a header unless you control the entire path from the client to the server, because proxies or load balancers are allowed to strip non-standard headers. 除非您控制从客户端到服务器的整个路径,否则应避免使用标头,因为允许代理或负载平衡器剥离非标准标头。

Include in the POST body a send_email=1 or send_email=0 param. 在POST正文中包含send_email=1send_email=0参数。 You'll extract that, and see what the user wants to do. 您将提取它,然后查看用户想要做什么。

Search "how to get POST variables in JAVA". 搜索“如何在JAVA中获取POST变量”。

Accessing post variables using Java Servlets 使用Java Servlet访问发布变量

You can do like this: 您可以这样:

  1. Add a new Java attribute(like boolean emailEnabled ) in your Java Request Object for your REST service. 在Java请求对象中为REST服务添加一个新的Java属性(如boolean emailEnabled )。

  2. Client side which invokes your REST service need to provide that parameter you added in your server side, you can set a default value for that too. 调用REST服务的客户端需要提供您在服务器端添加的参数,您也可以为其设置默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM