-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpost-request.go
75 lines (60 loc) · 2.44 KB
/
post-request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"bytes"
"encoding/json"
"github.com/dpapathanasiou/go-api"
"net/http"
"strings"
)
type Message struct {
Status string
Data []string
}
// The logPostData function accepts an http.ResponseWriter and http.Request
// object as input; it uses the http.Request object to confirm that the API
// request from the client is a POST, and then echoes back the variable
// name/value pairs in a JSON object (a more complex API server would
// actually do something with the POST data, of course).
func logPostData(w http.ResponseWriter, r *http.Request) string {
// prepare the default response, in case the request is invalid
m := Message{Status: "Sorry, there was a problem", Data: []string{}}
// this function only responds to POST requests
if "POST" == r.Method {
r.ParseForm()
// iterate over the data sent via a client POST request:
// k = the variable name
// v = the list of values corresponding to k
// for this example, we're just going to echo the data
// back as a single string message within the json object,
// just to prove we can get all names and variables correctly
var buffer bytes.Buffer // efficient way to concanenate strings
var postData []string
for k, v := range r.PostForm {
buffer.WriteString(k)
buffer.WriteString("=")
buffer.WriteString(strings.Join(v, ","))
postData = append(postData, buffer.String())
buffer.Reset()
}
m = Message{Status: "ok", Data: postData}
}
b, err := json.Marshal(m)
if err != nil {
panic(err) // no, not really
}
return string(b)
}
func main() {
handlers := map[string]func(http.ResponseWriter, *http.Request){}
handlers["/logger"] = func(w http.ResponseWriter, r *http.Request) {
api.Respond("application/json", "utf-8", logPostData)(w, r)
}
api.NewLocalServer(api.DefaultServerTransport, 9001, api.DefaultServerReadTimeout, false, handlers)
// To run the api server on a specific IP address, e.g., 192.168.1.1, use NewServer() instead:
//api.NewServer("192.168.1.1", api.DefaultServerTransport, 9001, api.DefaultServerReadTimeout, false, handlers)
// Another set of options are the transport layer (default is TCP) and FastCGI
// To run the api server as a UDP server on a specific IP address or domain, change the transport:
//api.NewServer("192.168.1.1", "udp", 9001, api.DefaultServerReadTimeout, false, handlers)
// The same example, but with FastCGI:
//api.NewServer("192.168.1.1", "udp", 9001, api.DefaultServerReadTimeout, true, handlers)
}