Use NewDecoder instead of read/unmarshal

This commit is contained in:
andig 2021-01-17 20:25:36 +01:00
parent 1c67b2fcc3
commit edd1b79332
2 changed files with 4 additions and 11 deletions

View file

@ -3,7 +3,6 @@ package semp
import (
"encoding/xml"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
@ -452,12 +451,7 @@ func (s *SEMP) allPlanningRequest() (res []PlanningRequest) {
func (s *SEMP) deviceControlHandler(w http.ResponseWriter, r *http.Request) {
var msg EM2Device
body, err := ioutil.ReadAll(r.Body)
if err == nil {
defer r.Body.Close()
err = xml.Unmarshal(body, &msg)
}
err := xml.NewDecoder(r.Body).Decode(&msg)
s.log.TRACE.Printf("recv: %+v", msg)
if err != nil {

View file

@ -72,12 +72,11 @@ func ReadBody(resp *http.Response) ([]byte, error) {
// DecodeJSON reads HTTP response and decodes JSON body if error is nil
func DecodeJSON(resp *http.Response, res interface{}) error {
b, err := ReadBody(resp)
if err == nil {
err = json.Unmarshal(b, &res)
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return StatusError{resp: resp}
}
return err
return json.NewDecoder(resp.Body).Decode(&res)
}
// New builds and executes HTTP request and returns the response