xmlenc

package
v0.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2015 License: MIT Imports: 3 Imported by: 0

README

xmlenc

This package provides XML encoding for Go-Relax.

Installation

Using "go get":

go get "github.com/codehack/go-relax/xmlenc"

Then import from source:

import "github.com/codehack/go-relax/xmlenc"

Usage

To accept and respond with xml, you must add an object to the Service.Encoders map.

package main

import (
	"github.com/codehack/go-relax"
	"github.com/codehack/go-relax/xmlenc"
	"net/http"
)

func main() {
	mysrv := relax.NewService("/api")

	// create and configure new encoder object
	enc := NewEncoderXML()
	enc.Indented = true

	// assign it to service "mysrv".
	// this maps "application/xml" media queries to this encoder.
	mysrv.Use(enc)

	// done. now you can continue with your resource routes etc...

	http.Handle(mysrv.Handler())
	log.Fatal(http.ListenAndServe(":8000", nil))
}
Options
encoder := &xmlenc.EncoderXML{Indented: true, MaxBodySize: 10000, AcceptHeader: "text/xml"}

Indented: boolean; set to true to encode indented XML. Default is false.

MaxBodySize: int; the maximum size (in bytes) of XML content to be read. Default is 4194304 (4MB)

AcceptHeader: the MIME media type expected in Accept header. Default is "application/xml"

ContentTypeHeader: the MIME media type, and optionally character set, expected in Content-Type header. Default is "application/xml;charset=utf-8"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EncoderXML

type EncoderXML struct {
	// MaxBodySize is the maximum size (in bytes) of XML content to be read (io.Reader)
	// Defaults to 4194304 (4MB)
	MaxBodySize int64

	// Indented indicates whether or not to output indented XML.
	// Defaults to false
	Indented bool

	// AcceptHeader is the media type used in Accept HTTP header.
	// Defaults to "application/xml"
	AcceptHeader string

	// ContentTypeHeader is the media type used in Content-Type HTTP header
	// Defaults to "application/xml;charset=utf-8"
	ContentTypeHeader string
}

EncoderXML implements the relax.Encoder interface. It encode/decodes XML.

func NewEncoderXML

func NewEncoderXML() *EncoderXML

NewEncoderXML returns an EncoderXML object. This function will initiallize the object with sane defaults, for use with Service.encoders. Returns the new EncoderXML object.

func (*EncoderXML) Accept

func (e *EncoderXML) Accept() string

Accept returns the media type for XML content, used in Accept header.

func (*EncoderXML) ContentType

func (e *EncoderXML) ContentType() string

ContentType returns the media type for XML content, used in the Content-Type header.

func (*EncoderXML) Decode

func (e *EncoderXML) Decode(reader io.Reader, v interface{}) error

Decode reads an XML payload (usually from Request.Body) and tries to set it to a variable v. If the payload is too large, with maximum EncoderXML.MaxBodySize, it will fail with error ErrBodyTooLarge Returns nil on success and error on failure.

func (*EncoderXML) Encode

func (e *EncoderXML) Encode(writer io.Writer, v interface{}) error

Encode will try to encode the value of v into XML. If EncoderJSON.Indented is true, then the XML will be indented with tabs. Returns the nil on success, and error on failure.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL