Skip to content

Commit

Permalink
json count
Browse files Browse the repository at this point in the history
  • Loading branch information
epomatti committed Jul 7, 2023
1 parent 7988a5f commit e92d3bb
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ type Order struct {
}

func jsonFunc(w http.ResponseWriter, r *http.Request) {
countStr := r.URL.Query().Get("count")
count := 1
if len(countStr) > 0 {
countTmp, err := strconv.Atoi(countStr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "Count argument is an invalid number: "+countStr)
return
} else {
count = countTmp
}
}
order := &Order{
Id: "001",
Description: "A very special order",
Expand All @@ -53,7 +65,11 @@ func jsonFunc(w http.ResponseWriter, r *http.Request) {
Date: time.Now(),
DeliveryDate: time.Now(),
}
data, _ := json.Marshal(order)
orders := []*Order{}
for i := 0; i < count; i++ {
orders = append(orders, order)
}
data, _ := json.Marshal(orders)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, string(data))
Expand Down

0 comments on commit e92d3bb

Please sign in to comment.