Thruster icon indicating copy to clipboard operation
Thruster copied to clipboard

[FEATURE REQUEST] Add example for parse and validate a JSON body and query params

Open Israel-Laguan opened this issue 1 year ago • 3 comments

The current doc works with a string body, and the example that would help, the error handle one, don't touch the body.

Filling a form is a basic example for servers, and we can have the most used RESTful CRUD functionality:

  • some data from a param (i.e. DELETE /user/:id), currently covered for example in:

https://github.com/thruster-rs/Thruster/blob/d80cf36ca3f8f40324dc5325a3c150aa5c833953/thruster/examples/mutable_state.rs#L37-L45

(Not sure if there is a "short form"? Maybe like a HashMap context.params?)

  • some data from a query param (i.e. GET /users?limit=12&page=2&query=Tom), available as the HashMap context.query_params but not shown in examples.

  • data from a body (i.e. in POST /user) with a form data like

{
  name: "Bob",
  surname: "Sponge",
  company: "Krusty Crab",
  skills: ["annoying giggles", "cook patties", "karate"],
  inventory: { clothes: ["pants", "shirt"], huntingNets: 2 }
}

(Note it has inner objects)

I am aware of context.get_body() but it is a String and most people just starting to know thruster would expect a way to parse into JSON.

I think that including examples that cover such cases, like the JSON body, would help people implement thruster for web servers or other cases.

Request

  • Include examples of parsing a JSON body
  • (OPTIONAL) Includes example validating required and optional fields in the body. Not sure if posible using serde?
  • Include an example of getting query params from a request

Israel-Laguan avatar Oct 05 '22 07:10 Israel-Laguan

Sorry for the delay, I'll get around to this and the other examples this weekend! Thanks for pointing it out -- I can't believe I don't have a json example 🤦‍♀️ .

trezm avatar Oct 07 '22 15:10 trezm

Please see #232 for examples!

trezm avatar Oct 11 '22 13:10 trezm

Solved in https://github.com/thruster-rs/Thruster/pull/232

Israel-Laguan avatar Oct 13 '22 08:10 Israel-Laguan

Can this be closed?

Israel-Laguan avatar Oct 25 '22 20:10 Israel-Laguan