【问题标题】:get fragment values in url获取 url 中的片段值
【发布时间】:2018-02-05 13:07:13
【问题描述】:

示例代码

func main() {
	fmt.Print("starting box web server...")
	http.HandleFunc("/", landing)
	http.HandleFunc("/handle", handler)
	http.ListenAndServe(connector_port, nil)
}
func landing(w http.ResponseWriter, r *http.Request) {
	fmt.Println("redirecting to login for authentication...")
	http.Redirect(w, r, "http://*****urlfortoken", http.StatusFound)
}
func handler(w http.ResponseWriter, r *http.Request) {
	bodyresnew_folder, _ := ioutil.ReadAll(r.Body)
	fmt.Println("response Body:", string(bodyresnew_folder))
	fmt.Println("1", r.GetBody)
	fmt.Println("2", r.URL.String())
	fmt.Println("3", r.URL.Fragment)
	fmt.Println("4", r.URL.Query().Get("access_token"))
	fmt.Println("inside handle function,", r.Form.Get("access_token"))
	fmt.Println("finished processing all files please close this server manually")
}

我尝试使用上面的代码从 URL 中获取片段,但没有成功。

使用的示例 URL 是:http://localhost:8080/handle#access_token=*1234$111&token_type=bearer&expires_in=3600&scope=onedrive.readwrite&user_id=hashed

现在,对于这样的 URL,我想在基本上是一个 http 处理程序的 handler 函数中获取片段 access_token 的值。

【问题讨论】:

  • 片段不发送到服务器。所以你不能“得到”它们:传入的请求不包含片段。
  • @Volker 哦谢谢你的信息

标签: go httphandler


【解决方案1】:

正如评论者已经指出的那样,片段不是您请求的一部分。片段可以是您的 HTML 标记的子集,由某个 ID 标识 - 这将是服务器返回的内容。

我想知道你为什么不使用 GET 参数?

那么你的网址可能是

http://localhost:8080/handle?access_token=*1234$111&token_type=bearer&expires_in=3600&scope=onedrive.readwrite&user_id=hashed

即在/handle 之后使用? 而不是#

然后您将正确看到您的access_token GET 参数:

inside handle function, 
1 <nil>
2 /handle?access_token=*1234$111&token_type=bearer&expires_in=3600&scope=onedrive.readwrite&user_id=hashed
3 
4 *1234$111

希望这会有所帮助。

【讨论】:

  • 我无法命令服务器发送带有参数而不是片段的 URL
  • 你的意思是客户。客户发送此
  • 所以我猜想出于某种原因你想将你的参数编码为片段ID。但它工作的机会很小。例如,片段标识符具有允许的特定语法。我相信现在是时候提出问题了——为什么要这样设计?哪些限制阻碍了您使用更常见、更直接的方式?
  • 它是获取microsoft onedrive的accesstoken的方式,我们将在fragment中获取accesstoken
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-20
相关资源
最近更新 更多