Golang WebAssembly (WASM) HTTP.NewRequest POST Method with CSRF Header - Golang Web Development

preview_player
Показать описание
In this Golang Web Development Series #15, we'll learn how to communicate from the backend API from a Golang WebAssembly exposed Golang's Function to the JS client-side with step by step guide here in Golang's Web Development Series.

#MaharlikansCode
#GolangWebDevelopment15
#GolangHTTPNewRequestPOSTMethod
#GolangTutorial
#LearnGolangWebDevelopment
#Golang
#LifeAsSoftwareDeveloper
#Maharlikans
#FilipinoSoftwareDeveloper

Get Linode Account:

Source Codes:
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"syscall/js"
"time"
)

func login(this js.Value, args []js.Value) interface{} {
jsDoc := js.Global().Get("document")
if !jsDoc.Truthy() {
}
username := jsDoc.Call("getElementById", "username")
if !username.Truthy() {
}
password := jsDoc.Call("getElementById", "password")
if !password.Truthy() {
}
isSiteKeepMe := jsDoc.Call("getElementById", "isSiteKeepMe")
if !isSiteKeepMe.Truthy() {
}

var pUserName string = username.Get("value").String()
var pPassword string = password.Get("value").String()
pIsSiteKeepMe, _ := strconv.ParseBool(isSiteKeepMe.Get("value").String())

if len(strings.TrimSpace(pUserName)) == 0 {
}

if len(strings.TrimSpace(pPassword)) == 0 {
}

if len(strings.TrimSpace(pUserName)) == 0 {
}

if len(strings.TrimSpace(pPassword)) == 0 {
}

csrfToken := jsDoc.Call("getElementById", "csrfToken")
if !csrfToken.Truthy() {
}
var pCSRFToken string = csrfToken.Get("value").String()
// Compose the JSON post payload to teh API endpoint.
payLoad := map[string]interface{}{
"username": pUserName,
"password": pPassword,
"isSiteKeepMe": pIsSiteKeepMe,
}
bytesRepresentation, err := json.Marshal(payLoad)
if err != nil {
}

// HTTP new request
client := &http.Client{}

req, err := http.NewRequest("POST", fmt.Sprintf(siteHost), bytes.NewBuffer(bytesRepresentation))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-CSRF-TOKEN", pCSRFToken)
if err != nil {
}

var isSuccess bool = false
c1 := make(chan map[string]interface{}, 1)
var result2 map[string]interface{}
go func() {
resp, _ := client.Do(req)
defer resp.Body.Close()
json.NewDecoder(resp.Body).Decode(&result2)
c1 lt- result2
}()

var result map[string]interface{}
go func() interface{} {
for {
timeout := make(chan bool, 1)
go func() {
time.Sleep(time.Second * 1)
timeout lt- true
}()

select {
case result = lt-c1:
i := result["IsSuccess"]
mStatus := fmt.Sprint(i)
isSuccess, _ = strconv.ParseBool(mStatus)

msg := ""
if !isSuccess {
} else {
}

return APIResponse(isSuccess, msg)
break
case lt-timeout:

}
}
}()
return nil
}

// APIResponse is a generic swal response back to the JS client side
func APIResponse(isSuccess bool, msg string) interface{} {
if !isSuccess {
return js.Global().Call("eval", msg)
}
return js.Global().Call("eval", msg)
}

func exposeGoFuncJS() {
js.Global().Set("login", js.FuncOf(login))
}

func main() {
fmt.Println("Welcome to Maharlikans WASM tutorials")
c := make(chan bool, 1)

// Start exposing this following Go functions to JS
exposeGoFuncJS()
lt-c
}
Рекомендации по теме