Batch is a simple Go library for handling multipart batch requests. It adheres to the HTTP Multipart Batched Request Format draft spec.
The draft spec is not well specified when it comes to whether or not upstream requests use http
or https
. The http.Client
expects either when it makes a request.
By default all requests made by Batch use http
. However, individual requests can set the x-use-https
header in the Multipart/Batch message to use https
.
Here's a complete example of a Batch request where the batch message uses the x-use-https
header.
POST / HTTP/1.1
Host: example.org
Content-Type: multipart/batch; type="application/http;version=1.1" boundary=batch
Mime-Version: 1.0
--batch
Content-Type: application/http;version=1.1
Content-Transfer-Encoding: binary
Content-ID: <[email protected]>
x-use-https: true
POST /example/application HTTP/1.1
Host: example.org
Content-Type: text/plain
Content-Length: 3
Foo
--batch--
Batch adheres to the http.Handler
interface and it can be provided directly to http.ListenAndServe
.
package main
import (
"net/http"
"github.com/iamatypeofwalrus/batch"
)
func main() {
b := batch.New()
http.ListenAndServe(":8080", b)
}
You can customize the HTTP Client that Batch uses to perform a single request by providing it with any struct that adheres to the batch.HTTPClient
interface. The http.Client
adheres to this interface.
You can provide Batch with a batch.Logger
in order to capture any error messages that occur when processing requests. log.Logger
adheres to this interface.
package main
import (
"log"
"net/http"
"os"
"github.com/iamatypeofwalrus/batch"
)
func main() {
l := log.New(os.Stdout, "", log.LstdFlags)
b := &batch.Batch{
Log: l,
Client: http.DefaultClient,
}
http.HandleFunc("/batch", b.ServeHTTP)
log.Println("listening on :8080")
http.ListenAndServe(":8080", nil)
}