web服务器指定路径下的get参数接收与处理
2018-02-13 10:25
375 查看
当我们使用go建立了服务器,那么一种常见的需求就摆在面前。如何给这个服务器的某个路径传递参数呢?我们研究一下URL传参的接收与处理。
对于http.Request发出的请求,我们需要使用到URL.Query().Get("XXX")
实例
首先建立一个 dollars 类型,用以保存货币数值。type dollars float32 对dollars建立一个String()方法,用以确定显示格式func (d dollars) String() string { return fmt.Sprintf("$%.2f", d) } 建立一个map字典,保存多种东西的价格。type MyHandler map[string]dollars 在http.Handler中处理路径和接收参数的操作func (self MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)
完整代码示例package main
import (
"fmt"
"net/http"
"log"
)
type dollars float32
func (d dollars) String() string { return fmt.Sprintf("$%.2f", d) }
type MyHandler map[string]dollars
func (self MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
switch req.URL.Path {
case "/list":
for item, price := range self {
fmt.Fprintf(w, "%s: %s\n", item, price)
}
case "/price":
item := req.URL.Query().Get("item")
//item2 := req.Form.Get("item")
price, ok := self[item]
if !ok {
w.WriteHeader(http.StatusNotFound) // 404
fmt.Fprintf(w, "no such item: %q\n", item)
return
}
fmt.Fprintf(w, "%s\n", price)
default:
w.WriteHeader(http.StatusNotFound) // 404
fmt.Fprintf(w, "no such page: %s\n", req.URL)
}
}
func main() {
handler := MyHandler{"shoes": 50, "socks": 5}
log.Fatal(http.ListenAndServe(":4000", handler))
} 程序运行后,直接访问http://localhost:4000/ 结果如下no such page: / 访问http://localhost:4000/list 结果如下:shoes: $50.00
socks: $5.00 访问http://localhost:4000/price 结果如下:no such item: "" 这个路径是需要正确参数的,所以需要访问http://localhost:4000/price?item=socks 结果如下:$5.00 http://localhost:4000/price?item=shoes 结果如下$50.00
对于http.Request发出的请求,我们需要使用到URL.Query().Get("XXX")
实例
首先建立一个 dollars 类型,用以保存货币数值。type dollars float32 对dollars建立一个String()方法,用以确定显示格式func (d dollars) String() string { return fmt.Sprintf("$%.2f", d) } 建立一个map字典,保存多种东西的价格。type MyHandler map[string]dollars 在http.Handler中处理路径和接收参数的操作func (self MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)
完整代码示例package main
import (
"fmt"
"net/http"
"log"
)
type dollars float32
func (d dollars) String() string { return fmt.Sprintf("$%.2f", d) }
type MyHandler map[string]dollars
func (self MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
switch req.URL.Path {
case "/list":
for item, price := range self {
fmt.Fprintf(w, "%s: %s\n", item, price)
}
case "/price":
item := req.URL.Query().Get("item")
//item2 := req.Form.Get("item")
price, ok := self[item]
if !ok {
w.WriteHeader(http.StatusNotFound) // 404
fmt.Fprintf(w, "no such item: %q\n", item)
return
}
fmt.Fprintf(w, "%s\n", price)
default:
w.WriteHeader(http.StatusNotFound) // 404
fmt.Fprintf(w, "no such page: %s\n", req.URL)
}
}
func main() {
handler := MyHandler{"shoes": 50, "socks": 5}
log.Fatal(http.ListenAndServe(":4000", handler))
} 程序运行后,直接访问http://localhost:4000/ 结果如下no such page: / 访问http://localhost:4000/list 结果如下:shoes: $50.00
socks: $5.00 访问http://localhost:4000/price 结果如下:no such item: "" 这个路径是需要正确参数的,所以需要访问http://localhost:4000/price?item=socks 结果如下:$5.00 http://localhost:4000/price?item=shoes 结果如下$50.00
相关文章推荐
- Python搭建Web服务器,与Ajax交互,接收处理Get和Post请求的简易结构
- android客户端采用Post和Get方式向web服务器传递参数
- Vue------第五天(Vue的Ajax请求,使用Axios,目前熟悉了一下,主要包括GET请求、POST请求、并发请求、请求参数的配置、服务器响应的结构以及对服务器响应错误的简单处理)
- web前端——ajax提交后台controller实现前端后台交互(3) 传多个参数 ,后台controller接收处理
- C# winform端 通过HttpWebRequest进行post和get请求,数据格式为json,后台java端接收,其中有关传输特殊字符(\t,\r,',\n,n)等处理
- android上传文件和参数到web服务器,php接收并保存
- 小程序get方式发送接收参数,与服务器通信-小程序支付开发4
- web项目使用Tomcat作为服务器,GET请求参数中文乱码
- servlet接收get带中文参数请求的处理方法
- web服务器(jetty)处理请求出现NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()I
- 如何利用form表单提交文件,和SSM如何接收文件参数,并写入到指定路径下的文件中
- 自己动手写web服务器二(处理get请求)
- web项目使用Tomcat作为服务器,GET请求参数中文乱码
- web前端——ajax提交后台controller实现前端后台交互(2) 传两个参数 ,后台controller接收处理
- 向指定 URL 发送GET,POST方法的请求(传入指定url和参数,服务器返回字符串数据)
- (6)服务器接收的get请求参数为什么都是null?
- javaweb中 ServletActionContext.getServletContext().getRealPath("")获得服务器路径
- Android 使用GET和PUT方式以及用HttpClient方式向web服务器提交参数
- wcf指定要序列化多个请求正文参数,但没有任何包装元素。如果没有包装元素,至多可序列化一个正文参数。请删除多余的正文参数,或将 WebGetAttribute/WebInvokeAttribute 的
- 解决intellij idea 运行tomcat,jetty等web服务器接收参数不是utf-8编码问题