Which REST operation (GET, PUT, or POST) for valid

2019-02-04 12:35发布

My users enter a few information fields in a iOS app. This information must be validated on my server, which has a RESTful API. After validation the UI of the iOS app changes to indicate the result.

Neither GET, PUT, or POST seem to be appropriate, because I'm not getting a resource, and neither is a resource created or updated.

What is the best fitting REST operation to implement this validation?

3条回答
霸刀☆藐视天下
2楼-- · 2019-02-04 13:21

I use the same scenario as you and use PUT for it. You have to ask yourself: "when I send the same request twice, does this make a different state on server?" If yes, use POST, if no use PUT.

查看更多
爷的心禁止访问
3楼-- · 2019-02-04 13:34

I recommend using a ValidationResource and two requests. Each instance of this resource represents the validation of a set of data. The workflow:

1. Create new ValidationResource

  • Request: POST /path/to/validations
    • data to validate as the body
  • Response: 201 Created
    • Location: /path/to/validations/<unique-id-of-this-validation>

2. Look up result

  • Request: GET /path/to/validations/<unique-id-of-this-validation>
  • Respons: 200 OK
    • body: {'valid': true} or {'valid': false}

This is a RESTful approach in which the Validation is a Resource with server state.

查看更多
趁早两清
4楼-- · 2019-02-04 13:39

My users enter a few information fields in a iOS app. This information must be validated on my server, which has a RESTful API. After validation the UI of the iOS app changes to indicate the result....I'm not getting a resource, and neither is a resource created or updated.

Since you aren't saving anything (not modifying any resource), I'd think this is technically more RPC than RESTful to me.

The following is my opinion, so don't take it as gospel:

If the information is simply being submitted and you're saying yes or no, and you're not saving it, I'd say POST is fine..

If information were actually being saved / updated, then choosing the proper HTTP method would be a lot more relevant.

POST = CREATE / SUBMIT (in an RPC context)
PUT = UPDATE (or CREATE if there is nothing to UPDATE)
查看更多
登录 后发表回答