#Which way of sending json?

6 messages · Page 1 of 1 (latest)

odd vector
#

1:

res, err := json.Marshal(data)
    if err != nil {
        app.logger.Println(err)
        http.Error(w, "There was an error marshalling the data", http.StatusInternalServerError)
        return
    }
    w.Header().Set("Content-Type", "application/json")
    w.Write([]byte(res))```

2.
```w.Header().Set("Content-Type", "application/json")
    w.WriteHeader(statusCode)
    json.NewEncoder(w).Encode(data)```

and why?
silver furnace
#

2

#

unnecessary to keep the entire payload in memory before sending it to the network

#

there's a reason w implements writer 🙂

odd vector
#

but what would happen on an error?

silver furnace