《Manning.Go.Web.Programming.2016.7.pdf》之form

HTML forms and Go

POST请求发送的键值对数据的格式是由HMTL form的content type指定的。在html中通过enctype指定如下:

<form action="/process" method="post" enctype="application/x-www-
form-urlencoded">
<input type="text" name="first_name"/>
<input type="text" name="last_name"/>
<input type="submit"/>
</form>

eenctype的默认值是application/x-www-form-urlencoded
,浏览器至少支持application/x-www-form-urlencodedmultipart/form-data
如果设置enctype为application/x-www-form-urlencoded,浏览器会把HTML form数据编码为一个长的查询字符串,和url编码类似,用&来分割键值对,键值对之前用=分开:

first_name=sau%20sheong&last_name=chang

如果enctype为multipart/form-data,数据编码为下面的格式

------WebKitFormBoundaryMPNjKpeO9cLiocMw
Content-Disposition: form-data; name="first_name"
sau sheong
------WebKitFormBoundaryMPNjKpeO9cLiocMw
Content-Disposition: form-data; name="last_name"
chang
------WebKitFormBoundaryMPNjKpeO9cLiocMw--

form

访问Request中数据的通用方法是:
1.调用ParseForm或ParseMultipartForm解析Request。
2.访问对应的Form,PostForm, MultipartForm
e
c测试html,enctype =application/x-www-form-urlencoded

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Go Web Programming</title>
</head>
<body>
<form action=http://127.0.0.1:8089/process?hello=world&thread=123
method="post" enctype="application/x-www-form-urlencoded">
<input type="text" name="hello" value="sau sheong"/>
<input type="text" name="post" value="456"/>
<input type="submit"/>
</form>
</body>
</html>

服务器测试代码

func processf(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    Debug(w, r.Form)
    Debug(w, r.PostForm)
    Debug(r.Form["hello"])
    Debug(r.FormValue("hello"))
    Debug(r.PostFormValue("hello"))
    // fmt.Fprintln(w, r.Form["hello"])
    // fmt.Fprintln(w, r.FormValue("hello"))
    // fmt.Fprintln(w, r.URL)
}

结果

364|&{0xc82018c100 0xc82019e540 0xc820156a00 false false 0xc820156a80 {0xc8200c7a00 map[] false false} map[] false 0 -1 0 false false [] 0 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] <nil>} map[hello:[sau sheong world] post:[456] thread:[123]]

365|&{0xc820196180 0xc8201a8620 0xc820194600 false false 0xc820194680 {0xc82018e5b0 map[] false false} map[] false 0 -1 0 false false [] 0 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] <nil>} map[hello:[sau sheong] post:[456]]

366|[sau sheong world]
367|sau sheong
368|sau sheong

可以看到如果form和url含有同样的key,会一起放在slice中,但是form的值优先于url。
postform只提供form的键值对,不包含url的。

把enctype改变为multipart/form-data

结果

364|&{0xc820196180 0xc820210e00 0xc8201f87c0 false false 0xc8201f8800 {0xc820232000 map[] false false} map[] false 0 -1 0 false false [] 0 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] <nil>} map[hello:[world] thread:[123]]

365|&{0xc820196180 0xc820210e00 0xc8201f87c0 false false 0xc8201f8800 {0xc820232000 map[] false false} map[] false 0 -1 0 false false [] 0 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] <nil>} map[]

366|[world]
367|world
368|

e
k可以看到只得到了url的键值对,由于PostForm只支持application/x-www-form-urlencoded,所以为空。

MultipartForm

d对于multipart/form-data要使用ParseMultipartForm解析。

r.ParseMultipartForm(1024)
fmt.Fprintln(w, r.MultipartForm)

MultipartForm不包含url键值对。

FormValue方法可以访问键值对无需调用ParseForm o或ParseMultipartForm,因为FormValue已经调用了。

FormValue只返回slice中的第一个值。u
如果enctype设置为multipart/form-data, FormValue和PostFormValue无法获取form的键值对,因为FormValue和PostFormValue对应的是Form和PostForm域,即使调用ParseMultipartForm,没有Form和PostForm域,所以无法获取。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Summary Go Web Programming teaches you how to build scalable, high-performance web applications in Go using modern design principles. Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. About the Technology The Go language handles the demands of scalable, high-performance web applications by providing clean and fast compiled code, garbage collection, a simple concurrency model, and a fantastic standard library. It's perfect for writing microservices or building scalable, maintainable systems. About the Book Go Web Programming teaches you how to build web applications in Go using modern design principles. You'll learn how to implement the dependency injection design pattern for writing test doubles, use concurrency in web applications, and create and consume JSON and XML in web services. Along the way, you'll discover how to minimize your dependence on external frameworks, and you'll pick up valuable productivity techniques for testing and deploying your applications. What's Inside Basics Testing and benchmarking Using concurrency Deploying to standalone servers, PaaS, and Docker Dozens of tips, tricks, and techniques About the Reader This book assumes you're familiar with Go language basics and the general concepts of web development. About the Author Sau Sheong Chang is Managing Director of Digital Technology at Singapore Power and an active contributor to the Ruby and Go communities. Table of Contents Part 1 Go and web applications Chapter 1 Go and web applications Chapter 2 Go ChitChat Part 2 Basic web applications Chapter 3 Handling requests Chapter 4 Processing requests Chapter 5 Displaying content Chapter 6 Storing data Part 3 Being real Chapter 7 Go web services Chapter 8 Testing your application Chapter 9 Leveraging Go concurrency Chapter 10 Deploying Go
Summary Go Web Programming teaches you how to build scalable, high-performance web applications in Go using modern design principles. Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. About the Technology The Go language handles the demands of scalable, high-performance web applications by providing clean and fast compiled code, garbage collection, a simple concurrency model, and a fantastic standard library. It's perfect for writing microservices or building scalable, maintainable systems. About the Book Go Web Programming teaches you how to build web applications in Go using modern design principles. You'll learn how to implement the dependency injection design pattern for writing test doubles, use concurrency in web applications, and create and consume JSON and XML in web services. Along the way, you'll discover how to minimize your dependence on external frameworks, and you'll pick up valuable productivity techniques for testing and deploying your applications. What's Inside Basics Testing and benchmarking Using concurrency Deploying to standalone servers, PaaS, and Docker Dozens of tips, tricks, and techniques About the Reader This book assumes you're familiar with Go language basics and the general concepts of web development. About the Author Sau Sheong Chang is Managing Director of Digital Technology at Singapore Power and an active contributor to the Ruby and Go communities. Table of Contents Part 1 Go and web applications Chapter 1 Go and web applications Chapter 2 Go ChitChat Part 2 Basic web applications Chapter 3 Handling requests Chapter 4 Processing requests Chapter 5 Displaying content Chapter 6 Storing data Part 3 Being real Chapter 7 Go web services Chapter 8 Testing your application Chapter 9 Leveraging Go concurrency Chapter 10 Deploying Go
manning.microservices.patterns是指微服务架构模式方面的内容。微服务是一种架构风格,它将一个应用程序拆分为一组小的、独立的服务,每个服务都可以独立部署、扩展和维护。manning.microservices.patterns通过提供一些常见的模式,帮助开发人员构建可靠、可伸缩、可维护的微服务架构。 首先,manning.microservices.patterns介绍了服务拆分的模式。它提供了一些指导原则,帮助开发人员确定应该将哪些功能拆分为独立的服务,以及如何定义服务边界。这样可以确保每个服务都专注于一个明确的业务领域,并且可以独立地开发和部署。 其次,manning.microservices.patterns讨论了服务间通信的模式。微服务架构中,各个服务需要相互通信来实现业务流程。这本书介绍了一些常见的通信模式,例如同步调用、异步消息传递和事件驱动架构。开发人员可以根据实际需求选择适合的通信方式。 此外,manning.microservices.patterns还介绍了服务发现和负载均衡的模式。由于微服务架构中的服务数量庞大,需要有一种方法来发现和跟踪可用的服务实例。这本书提供了一些模式,如服务注册与发现、负载均衡和断路器模式,帮助开发人员实现可靠的服务发现和负载均衡机制。 最后,manning.microservices.patterns还探讨了监控和故障处理的模式。微服务架构中,每个服务都可以独立部署和维护,因此需要一种方法来监控服务的健康状况,并及时处理故障。这本书介绍了一些常见的监控和故障处理模式,例如服务指标监控、日志聚合和异常处理模式。 总之,manning.microservices.patterns是一本关于微服务架构模式的书籍,它提供了一些常见的模式,帮助开发人员构建可靠、可伸缩、可维护的微服务架构。这些模式涵盖了服务拆分、服务间通信、服务发现和负载均衡、监控和故障处理等方面,可以帮助开发人员在构建微服务架构时更加高效地解决各种技术挑战。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值