本文整理汇总了Golang中encoding/xml.NewEncoder函数的典型用法代码### 示例。如果您正苦于以下问题:Golang NewEncoder函数的具体用法?Golang NewEncoder怎么用?Golang NewEncoder使用的例子?那么恭喜您, 这里精选的函数代码### 示例或许可以为您提供帮助。

在下文中一共展示了NewEncoder函数的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。

示例1: ServeAPI

func ServeAPI(w http.ResponseWriter, req *http.Request, g *git, ref string, maxCommits int) (err error) {
    // First, determine the encoding and error if it isn't appropriate
    // or supported. To do this, we need to check the api value and
    // Accept header. We also want to include the Content-Type.
    var e encoder
    var c string // The Content-Type field in the http.Response
    switch req.FormValue("api") {
    case "json":
        // The json.Encoder type implements our private encoder
        // interface, because it has the function Encode().
        c = "application/json"
        e = json.NewEncoder(w)
    case "xml":
        // Same as above.
        c = "application/xml"
        e = xml.NewEncoder(w)
    }
    // If the api field wasn't submitted in the form, we should still
    // check the Accept header.
    accept := strings.Split(strings.Split(
        req.Header.Get("Accept"), ";")[0],
        ",")
    if e == nil && len(accept) != 0 {
        // Now we must loop through each element in accept, because
        // there can be multiple values to the Accept key. As soon as
        // we find an acceptable encoding, break the loop.
        for _, a := range accept {
            switch {
            case strings.HasSuffix(a, "/json"):
                e = json.NewEncoder(w)
            case strings.HasSuffix(a, "/xml"):
                e = xml.NewEncoder(w)
            }
            if e != nil {
                c = a // Set the content type appropriately.
                break
            }
        }
    }
    // If the encoding is invalid or not provided, return this error.
    if e == nil {
        return InvalidEncodingError
    }

    // If an encoding was provided, prepare a response.
    r := &APIResponse{
        GroveOwner:  gitVarUser(),
        HEAD:        g.SHA("HEAD"),
        Description: g.GetBranchDescription(ref),
        Commits:     g.Commits(ref, maxCommits),
    }
    // Set the Content-Type appropriately in the header.
    w.Header().Set("Content-Type", c)

    // Finally, encode to the http.ResponseWriter with whatever
    // encoder was selected.
    return e.Encode(r)
}

开发者ID:alexander-bauer,项目名称:grove,代码行数:58,代码来源:api.go

示例2: Xml

func (r *Response) Xml(o interface{}) error {
    writeContentType(r.w, xmlContentType)
    if r.gzipOn {
        w := gzip.NewWriter(r.w)
        defer w.Close()
        defer w.Flush()
        err := xml.NewEncoder(w).Encode(o)
        return err
    }
    return xml.NewEncoder(r.w).Encode(o)
}

开发者ID:RoySusanoo,项目名称:httpmux,代码行数:11,代码来源:response.go

示例3: serveHTTP

func (srv *Server) serveHTTP(w http.ResponseWriter, req *http.Request) {
    req.ParseForm()
    srv.mutex.Lock()
    defer srv.mutex.Unlock()
    action := req.FormValue("Action")
    if action == "" {
        srv.error(w, &iam.Error{
            StatusCode: 400,
            Code:       "MissingAction",
            Message:    "Missing action",
        })
    }
    if a, ok := actions[action]; ok {
        reqId := fmt.Sprintf("req%0X", srv.reqId)
        srv.reqId++
        if resp, err := a(srv, w, req, reqId); err == nil {
            if err := xml.NewEncoder(w).Encode(resp); err != nil {
                panic(err)
            }
        } else {
            switch err.(type) {
            case *iam.Error:
                srv.error(w, err.(*iam.Error))
            default:
                panic(err)
            }
        }
    } else {
        srv.error(w, &iam.Error{
            StatusCode: 400,
            Code:       "InvalidAction",
            Message:    "Invalid action: " + action,
        })
    }
}

开发者ID:fsouza,项目名称:go-iam,代码行数:35,代码来源:server.go

示例4: Process

// Process executes the request to create a new report.
func (r *ICreateReq) Process() (*ICreateReqResp, error) {
    // log.Printf("%s\n", r)
    fail := func(err error) (*ICreateReqResp, error) {
        response := ICreateReqResp{
            Message:  "Failed",
            ID:       "",
            AuthorID: "",
        }
        return &response, err
    }

    var payload = new(bytes.Buffer)
    {
        enc := xml.NewEncoder(payload)
        enc.Indent("  ", "    ")
        enc.Encode(r)
    }
    // log.Printf("Payload:\n%v\n", payload.String())

    url := "http://localhost:5050/api/"
    resp, err := http.Post(url, "application/xml", payload)
    if err != nil {
        return fail(err)
    }

    var response ICreateReqResp
    err = xml.NewDecoder(resp.Body).Decode(&response)
    if err != nil {
        return fail(err)
    }

    return &response, nil
}

开发者ID:radhikapc,项目名称:open311_gateway,代码行数:34,代码来源:create.go

示例5: HandleUnauthorized

func HandleUnauthorized(c context.Context, w http.ResponseWriter, e error) {
    w.WriteHeader(http.StatusUnauthorized)

    // Error can be nil.
    errStr := "You are not authorized to access this page."
    if e != nil {
        errStr = e.Error()
        c.Errorf("HandleUnauthorized: %v", errStr)
    }

    data := &errResult{
        Err: errStr,
        C:   c,
    }

    switch w.Header().Get("Content-Type") {
    case "application/json":
        if err := json.NewEncoder(w).Encode(data); err != nil {
            c.Errorf("json.Encode failed: %v", err)
            return
        }
    case "text/xml":
        w.Write([]byte(xml.Header))
        if err := xml.NewEncoder(w).Encode(data); err != nil {
            c.Errorf("xml.Encode failed: %v", err)
            return
        }
    default:
        HandleTemplate(c, w, UnauthorizedTpl, data)
    }
}

开发者ID:vmihailenco,项目名称:appengine,代码行数:31,代码来源:httphelper.go

最后编辑: kuteng  文档更新时间: 2021-08-23 19:14   作者:kuteng