esterfreyja 1 year ago
commit 6af4ddbf78

@ -6,7 +6,7 @@ import (
_ "github.com/lib/pq" _ "github.com/lib/pq"
) )
func main() { func mainORM() {
connStr := "user=ada password='TR42ma31&*' dbname=dbada sslmode=disable" connStr := "user=ada password='TR42ma31&*' dbname=dbada sslmode=disable"
db, err := sql.Open("postgres", connStr) db, err := sql.Open("postgres", connStr)
if err != nil { if err != nil {

Binary file not shown.

@ -5,24 +5,29 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"log" "log"
"io/ioutil"
) )
type Data struct { type Data struct {
identifier string Identifier string
password string Password string
} }
func handleRequest(w http.ResponseWriter, r *http.Request) { func handleRequest(w http.ResponseWriter, r *http.Request) {
var data Data body, err := ioutil.ReadAll(r.Body)
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil { if err != nil {
http.Error(w,err.Error(), http.StatusBadRequest) fmt.Printf("body read KO")
return
} }
fmt.Fprintf(w, "Data: %+v", data) var data Data
err = json.Unmarshal(body, &data)
if err != nil {
fmt.Printf("deserialis KO")
}
fmt.Printf("%+v\n", data)
} }
func main() { func main() {
mainORM()
http.HandleFunc("/", handleRequest) http.HandleFunc("/", handleRequest)
log.Fatal(http.ListenAndServe(":8088", nil)) log.Fatal(http.ListenAndServe(":8088", nil))
} }

@ -9,33 +9,36 @@ import (
) )
type Data struct { type Data struct {
identifier string Identifier string
password string Password string
} }
func main() { func main() {
url := "http://localhost:8088" // Remplacez par l'URL de votre choix url := "http://localhost:8088" // Remplacez par l'URL de votre choix
data := Data { data := Data {
identifier: "machevaldo", Identifier: "machevaldo",
password: "superPassword"} Password: "superPassword"}
jsonData, err := json.Marshal(data) jsonData, err := json.Marshal(data)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
var m Data
err1 := json.Unmarshal(jsonData, &m)
if err1 != nil {
log.Fatal(err)
}
fmt.Println(m)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer resp.Body.Close() defer resp.Body.Close()
fmt.Println("Response Status:", resp.Status) fmt.Println("Response Status:", resp.Status)
} }
Loading…
Cancel
Save