curl - How do I accept POST requests from Go and write output to a file? -
first of all, i'm trying create logging service in go, lightweight server accepting post requests log data service. i'm writing service in go because supposed fast , handle lot of post requests @ once. logic sound there?
anyways, issue. i'm sending post requests test: curl -h "content-type: application/json" -x post -d '{"hello":"world"}' http://localhost:8080/log
and here go script far:
package main import ( "fmt" "log" "net/http" ) func logger(w http.responsewriter, r *http.request) { r.parseform() fmt.println(r.form) fmt.println(r.formvalue("hello")) } func main() { http.handlefunc("/log", logger) log.fatal(http.listenandserve(":8080", nil)) }
it outputting: map []
why that? how write post data file on server?
thank much! w
r.method
contains type of received request (get, post, head, put, etc.).
you can read data r.body
ioutil
, write data file same package.
this handling post method, reading data , writing file (output.txt).
if r.method == "post" { body, err := ioutil.readall(r.body) if err != nil { fmt.errorf("error during reading body: %v", err) } if err := ioutil.writefile("output.txt", body, 0644); err != nil { fmt.errorf("error during writing data: %v", err) } }
Comments
Post a Comment