Created
January 16, 2018 14:59
-
-
Save eoinahern/c6f930fa9ebfe275dd79109a6ae605b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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