Add option for GET request for simple barcodes

This commit is contained in:
Fred Boniface 2023-09-08 10:41:51 +01:00
parent 7171bffc5e
commit 99a1a25254
2 changed files with 62 additions and 9 deletions

View File

@ -4,7 +4,12 @@
{{ end }} {{ end }}
{{ define "body" }} {{ define "body" }}
<p>API Requests can be made by making a POST request to /generate</p> <p>For simple textual QR, Aztec or DataMatrix codes, a GET request can be made as follows:</p>
<code>/generate?type=[qr|aztec|datamatrix]&text=[YOUR_TEXT]</code>
<p>Dimensions will default to 300x300, and ECC level to 2.</p>
<p>This is also the format for URL barcodes</p>
<p>For other barcode types and custom options, API Requests can be made by making a POST request to /generate</p>
<p>The request body should be JSON similar to the below</p> <p>The request body should be JSON similar to the below</p>
@ -35,6 +40,7 @@
<p>The response will be a PNG image</p> <p>The response will be a PNG image</p>
<h2>Formatted Barcodes</h2> <h2>Formatted Barcodes</h2>
<p>Formatted barcodes are under development, this API endpoint <code>/generate/format</code> will produce a QR, Aztec or Datamatrix barcode with one of the following formats:</p> <p>Formatted barcodes are under development, this API endpoint <code>/generate/format</code> will produce a QR, Aztec or Datamatrix barcode with one of the following formats:</p>
@ -47,4 +53,5 @@
<li>Phone Number</li> <li>Phone Number</li>
</ul> </ul>
<p>Note that URLs have no special formatting and can be produced as plain text barcodes using the <code>/generate</code> endpoint</p> <p>Note that URLs have no special formatting and can be produced as plain text barcodes using the <code>/generate</code> endpoint</p>
<p>This endpoint is not yet available</p>
{{ end }} {{ end }}

View File

@ -2,6 +2,7 @@ package web
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"image" "image"
"image/png" "image/png"
@ -12,19 +13,30 @@ import (
"github.com/boombuler/barcode" "github.com/boombuler/barcode"
) )
// Generates a barcode from the data and options provided in the request body // Generates a barcode from the data and options provided in the request
func generateBarcode(w http.ResponseWriter, r *http.Request) { func generateBarcode(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Use a POST request for barcode generation", http.StatusMethodNotAllowed) if r.Method != http.MethodPost && r.Method != http.MethodGet {
http.Error(w, "Use a POST or GET request for barcode generation. See the help page for more information", http.StatusMethodNotAllowed)
return return
} }
var req BarcodeRequest var req *BarcodeRequest
var err error
if r.Method == http.MethodGet {
req, err = generateFromGet(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
} else {
err := json.NewDecoder(r.Body).Decode(&req) err := json.NewDecoder(r.Body).Decode(&req)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
}
parameters := generation.Parameters{ parameters := generation.Parameters{
Format: generation.BarcodeType(req.BarcodeType), Format: generation.BarcodeType(req.BarcodeType),
@ -66,3 +78,37 @@ func generateBarcode(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Error streaming barcode", http.StatusInternalServerError) http.Error(w, "Error streaming barcode", http.StatusInternalServerError)
} }
} }
func generateFromGet(r *http.Request) (*BarcodeRequest, error) {
//var parameters generation.Parameters
if r.Method == http.MethodGet {
validTypes := map[string]bool{
"qr": true,
"aztec": true,
"datamatrix": true,
}
typeParam := r.URL.Query().Get("type")
content := r.URL.Query().Get("text")
if typeParam == "" || content == "" {
return nil, errors.New("both 'type' and 'text' parameters are required")
}
if !validTypes[typeParam] {
return nil, errors.New("'type' must be 'qr', 'aztec', or 'datamatrix'")
}
barcodeReq := &BarcodeRequest{
BarcodeType: typeParam,
Width: 300,
Height: 300,
ECCLevel: 2,
Content: content,
}
return barcodeReq, nil
} else {
return nil, errors.New("incorrect request type passed to function")
}
}