如何在后端处理PUT HTTP请求中的可选字段的最佳方式?

huangapple go评论96阅读模式
英文:

Whats the best way to handle optional field in PUT http request at backend

问题

I couldn't find developers guide for a PUT call. What should we do if in a PUT request an optional value is passed as null. Do we set it to null in db or we retain the previous value in db if any? The issue with this approach is we won't be able to update the values to nullify optional fields once created with value.

Eg.
For the same Entity suppose Student table, I have a nullable column of name suppose student_optional_subject. Now in POST student_optional_subject is set to value "calculus". Again a PUT request is made with student_optional_subject as null in the request body(i,e the field is missing in the request as it is an optional field).. shall I retain "calculus" in student_optional_subject or set it to null.

英文:

I couldn't find developers guide for a PUT call. What should we do if in a PUT request an optional value is passed as null. Do we set it to null in db or we retain the previous value in db if any? The issue with this approach is we won't be able to update the values to nullify optional fields once created with value.

Eg.
For the same Entity suppose Student table, I have a nullable column of name suppose student_optional_subject. Now in POST student_optional_subject is set to value "calculus". Again a PUT request is made with student_optional_subject as null in the request body(i,e the field is missing in the request as it is an optional field).. shall I retain "calculus" in student_optional_subject or set it to null..

答案1

得分: 9

如果在PUT请求中某个值为null,它应该被保存为null。不应该被忽略,因为它是可选的。

PUT方法的定义方式是,它应该始终是资源的完全更新。如果我进行了一次PUT,然后再进行一次GET请求来获取相同资源的表示,那么我从GET请求中接收到的表示应该等同于发送到PUT请求中的表示。

如果忽略了null字段,那就被称为部分更新。PUT不支持部分更新。这是构建REST API时人们经常犯的错误之一。

如果你想要进行部分更新,你可以使用PATCH方法。然而,要小心,因为比起PUT,PATCH被错误使用的情况更加常见。如果你使用PATCH,应该使用诸如application/patch+json或application/merge-patch+json之类的差异媒体类型。

英文:

If a value in a PUT request is null, it should be saved as null. It should not be ignored because it's optional.

The way the PUT method is defined, it should always be a full update of the resource. If I do a PUT and then a GET of the same resource, the representation I receive from the GET should be equivalent to representation sent in the PUT.

If you ignore null fields, that's known as a partial update. PUT does not support partial updates. This is one of the most common things people get wrong when building REST APIs.

If you want to do partial updates, you can use the PATCH method. However, be careful because PATCH is used incorrectly even more often than PUT. If you use PATCH, you should be using a diff media type such as application/patch+json or application/merge-patch+json.

答案2

得分: -1

如果你有一个学生表,学生的姓名应该是必填项,没有理由让学生的姓名是可选的。如果有一个学生存在,应该可以理解他们会有与之关联的姓名。学生表中的每个条目可以与一个主键关联,例如自增的 id 或随机生成的 64/128 位唯一标识 uuid。在这一点上,你应该有另一张名为 Subject 的表。如果 PUT 请求中不包含 student_subject,那么就不要创建新的 Subject 记录。但是,如果包含了 student_subject,则为给定的 Student 创建一个新的 StudentSubject,其中关联是 iduuid。这样,学生可以有课程或没有课程,但是 Student 表不关心他们是否有课程。

CREATE TABLE `student` (
    id int(11) NOT NULL AUTO_INCREMENT,
    name varchar(128),
    PRIMARY KEY (id)
);

CREATE TABLE `subject` (
    id int(11) NOT NULL AUTO_INCREMENT,
    student_id int(11),
    title varchar(64),
    PRIMARY KEY (id)
);
英文:

If you have a Student table, there should be no reason why the name of a student should be optional. If a Student exists, it should be understandable that they have a name associated with it. Each entry in the Student table could be associated with a primary-key like id that auto increments or uuid that is a randomly generated 64/128 bit unique id. At that point you should have another table called Subject. If the PUT doesn't contain a student_subject then you don't create a new Subject row. However, if it does, create a new StudentSubject for the given Student where the association is the id or uuid. This way, students can still have subjects or no subjects, but the Student table doesn't care if they do or not.

CREATE TABLE `student` (
    id int(11) NOT NULL AUTO_INCREMENT,
    name varchar(128),
    PRIMARY KEY (id)
);

CREATE TABLE `subject` (
    id int(11) NOT NULL AUTO_INCREMENT
    student_id int(11)
    title varchar(64)
    PRIMARY KEY (id)
);

huangapple
  • 本文由 发表于 2020年7月31日 20:50:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63192172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定