Go語言 擷取get、post參數

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

 

在貼代碼之前如果能先理解一下golang http.request的三個屬性Form、PostForm、MultipartForm應該能較好的理解代碼,下面摘錄一下。

以上簡要翻譯一下:

Form:儲存了post、put和get參數,在使用之前需要調用ParseForm方法。

PostForm:儲存了post、put參數,在使用之前需要調用ParseForm方法。

MultipartForm:儲存了包含了檔案上傳的表單的post參數,在使用前需要調用ParseMultipartForm方法。

 

 

 

 

一、擷取GET參數

 

query := req.URL.Query()get_act := query["act"][0]fmt.Println(get_act)

 

 

二、擷取POST參數

 

2.1 擷取單個POST欄位值

post_act := req.PostFormValue("act")fmt.Println(post_act)

 

2.2 擷取多個POST欄位值

如果我們在提交的時候,有兩個一樣input-name怎麼辦呢?如下的html代碼

<form method="POST" action="login?act=in"><input type="text" name="username" class="text" /><input type="text" name="username" class="text" /><input type="submit" name="login_button" value="登入"></form>

 

這時候就要把post傳輸的參數當成數組,通過PostFormValue就只能獲得第一個input的內容。怎麼能兩個都擷取到呢?

post_act := req.PostForm["username"]fmt.Println(post_act)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.