A
A
Andrey2018-11-22 22:26:03
go
Andrey, 2018-11-22 22:26:03

How to make optional post request parameters?

Good evening!
There is a web service that receives post-requests to one of the addresses, which are designed to filter data.
Filtering parameters come in the form of json.
now works like:

type ItemsFilters struct {
    ItemIds []int `json:"item_ids"`
    ParamA bool `json:"param_a"`
    ParamB bool `json:"param_b"`
    ParamC bool `json:"param_c"`
}

var filters ItemsFilters
context.bindJSON(&filters)

The front, of course, can use one service in a bunch of different places and no one will send a full set of parameters, you need to filter by what came and ignore everything else.
The sad thing is that if you send only the item_ids array, then the other 3 parameters == false and it's completely incomprehensible how to find out if they actually came false or if this is the default value.
Yes, you can also send the filters array, in which you list the active filters and rely on it, but even in your thoughts it looks like complete crap ..
Thanks in advance :-)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2018-11-22
@VladimirAndreev

I like Dasha Tsiklauri 's version more, but this is also a working version:

type FilterPost struct {
  StationIds interface{} `json:"station_ids"`
  Params     struct {
    L interface{} `json:"l"`
    X interface{} `json:"x"`
    Z interface{} `json:"z"`
  } `json:"params"`
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question