Programming/golang2022. 9. 2. 15:31

오랫만에 HTML 하니 다 까먹었네..

html 에서는 아래와 같이 post로 넘겨줄 변수 명은 name에 기재하고 form method를 post로 해주면 끝!

다만 action은 생략될 수 있으므로 처리해야할 페이지의 링크를 기록해주면 된다.

<form method="post" action="url">
<intut type="text" name="username">
</form>

 

func loginHandler(wr http.ResponseWriter, r *http.Request) {
        r.ParseForm()
        switch r.Method {
                case http.MethodPost: // 조회
                        fmt.Println(r)
                        fmt.Println(r.Form)
                        fmt.Println(r.PostForm)
                        fmt.Fprintln(wr, r.Form)
        }
}

[링크 : https://dksshddl.tistory.com/entry/Go-web-programming-request-처리-및-response-작성]

 

ParseForm() 을 실행하지 않으면, r.Form이 업데이트 되지 않아 내용이 조회가 되지 않는다.

ParseForm populates r.Form and r.PostForm.

For all requests, ParseForm parses the raw query from the URL and updates r.Form.

For POST, PUT, and PATCH requests, it also reads the request body, parses it as a form and puts the results into both r.PostForm and r.Form. Request body parameters take precedence over URL query string values in r.Form.

If the request Body's size has not already been limited by MaxBytesReader, the size is capped at 10MB.

For other HTTP methods, or when the Content-Type is not application/x-www-form-urlencoded, the request Body is not read, and r.PostForm is initialized to a non-nil, empty value.

ParseMultipartForm calls ParseForm automatically. ParseForm is idempotent.

[링크 : https://pkg.go.dev/net/http#Request.ParseForm]

 

html post를 직접 하는건데 유용한(?) 라이브러리가 보여서 링크!

import "encoding/json"
import "encoding/xml"

[링크 : http://golang.site/go/article/103-HTTP-POST-호출]

'Programming > golang' 카테고리의 다른 글

golang 쿠키  (0) 2022.09.02
golang http redirect  (0) 2022.09.02
golang http.HandleFunc(pattern)  (0) 2022.08.31
golang mariadb 연동  (0) 2022.08.30
golang channel  (0) 2022.08.18
Posted by 구차니