【问题标题】:Is it restful to have request body with optional parameters that will be required for some calls请求正文带有一些调用所需的可选参数是否令人放松
【发布时间】:2018-04-23 07:31:36
【问题描述】:

我想为支付构建一个 rest api,但请求正文将具有基于支付类型的参数。我不确定是否有一些支付类型需要的可选参数。这是示例端点详细信息,

POST /clients/{id}/payments/{type}

{
    amount: integer,
    card_details: {
        number: string,
        expiry: string
    },
    bank_details: {
        bsb: string,
        account_number: string
    },
    reference: string
}

bank_details 和 card_details 并非一直都需要,但是如果支付类型是信用卡,则需要信用卡详细信息,如果类型是银行存款,则需要银行详细信息。 我觉得这种方法并不理想。有没有更好的方法来实现这一点,或者采用这种方法可以吗? 希望我已经用所需的所有细节提出了这个问题。非常感谢您的建议。

【问题讨论】:

    标签: rest api restful-architecture


    【解决方案1】:

    bank_detailscard_details 并非一直都需要,但如果支付类型是信用卡,则需要card_details,如果类型是银行存款,则需要bank_details

    据我了解,您的应用程序允许客户端创建具有不同表示的不同资源。这很好。根据RFC 7231POST方法请求目标资源根据资源自身特定语义处理请求中包含的表示。

    【讨论】:

    • 感谢您的快速回复。这很有帮助:)
    猜你喜欢
    • 2012-10-03
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 2011-11-11
    • 2012-05-07
    • 2012-08-25
    • 2021-06-17
    相关资源
    最近更新 更多