Skip to content

Instantly share code, notes, and snippets.

@eoinahern
Created January 16, 2018 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eoinahern/c6f930fa9ebfe275dd79109a6ae605b2 to your computer and use it in GitHub Desktop.
Save eoinahern/c6f930fa9ebfe275dd79109a6ae605b2 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"encoding/json"
"log"
"net/http"
"os"
)
type Episode struct {
PodID uint `json:"podid"`
Created string `json:"created" `
Updated string `json:"updated" `
URL string `json:"url"`
Downloads int32 `json:"downloads" `
Blurb string `json:"blurb"`
Data os.File `json:"data" `
}
func main() {
myfile, err := os.Create("./blah.txt")
episode := Episode{
PodID: 5,
Created: "stuff",
Updated: "stuff",
URL: "stuff",
Blurb: "a blurb",
Data: *myfile,
}
var buf bytes.Buffer
err = json.NewEncoder(&buf).Encode(episode)
if err != nil {
log.Fatal(err)
}
client := http.Client{}
req, err := http.NewRequest(http.MethodPost, "http://localhost:8080/upload", &buf)
req.Header.Set("Content-Type", "application/json")
if err != nil {
log.Println(err)
}
client.Do(req)
}
func (e *UploadEpisodeHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var episode models.Episode
err := json.NewDecoder(req.Body).Decode(&episode)
if err != nil {
http.Error(w, "error", http.StatusInternalServerError)
return
}
fileInfo, err := episode.Data.Stat()
fmt.Println(fileInfo.Name())
fmt.Println(fileInfo.Size())
fmt.Println(fileInfo.ModTime())
fmt.Println(fileInfo.Sys())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment