英文:
Parse a url with # in GO
问题
所以我收到了一个发送到我的服务器的请求,看起来有点像这样:
http://localhost:8080/#access_token=tokenhere&scope=scopeshere
但我似乎找不到从URL中解析令牌的方法。
如果#符号是?符号,我可以像解析标准查询参数一样解析它。
我尝试过获取斜杠后面的所有内容,甚至是完整的URL,但没有成功。
非常感谢任何帮助。
编辑:
所以我现在解决了这个问题,正确的答案是在GO中实际上无法做到这一点。所以我制作了一个简单的包,在浏览器端完成解析,然后将令牌发送回服务器。
如果你想在GO中进行本地Twitch API操作,请查看一下:
https://github.com/SimplySerenity/twitchOAuth
英文:
So I'm receiving a request to my server that looks a little something like this
http://localhost:8080/#access_token=tokenhere&scope=scopeshere
and I can't seem to find a way to parse the token from the url.
If the # were a ? I could just parse it a standard query param.
I tried to just getting everything after the / and even the full URL, but with no luck.
Any help is greatly appreciated.
edit:
So I've solved the issue now, and the correct answer is you can't really do it in GO. So I made a simple package that will do it on the browser side and then send the token back to the server.
Check it out if you're trying to do local twitch API stuff in GO:
答案1
得分: 6
锚点部分通常不会由客户端发送到服务器。
例如,浏览器不会发送它。
英文:
Anchor part is not even (generally) sent by a client to the server.
Eg, browsers don't send it.
答案2
得分: 5
使用Go语言的net/url包来解析URL:https://golang.org/pkg/net/url/
注意:你应该使用Authorization头部来发送认证令牌。
以下是从你的示例URL中提取数据的示例代码:
package main
import (
"fmt"
"net"
"net/url"
)
func main() {
// 带有哈希的URL
s := "http://localhost:8080/#access_token=tokenhere&scope=scopeshere"
// 解析URL并确保没有错误。
u, err := url.Parse(s)
if err != nil {
panic(err)
}
// ---> 这里是你将获取URL哈希的地方
fmt.Println(u.Fragment)
fragments, _ := url.ParseQuery(u.Fragment)
fmt.Println("Fragments:", fragments)
if fragments["access_token"] != nil {
fmt.Println("Access token:", fragments["access_token"][0])
} else {
fmt.Println("Access token not found")
}
// ---> 从URL获取其他数据:
fmt.Println("\n\nOther data:\n")
// 访问方案很简单。
fmt.Println("Scheme:", u.Scheme)
// `Host`包含主机名和端口(如果有)。使用`SplitHostPort`来提取它们。
fmt.Println("Host:", u.Host)
host, port, _ := net.SplitHostPort(u.Host)
fmt.Println("Host without port:", host)
fmt.Println("Port:",port)
// 要以`k=v`格式获取查询参数的字符串,请使用`RawQuery`。您还可以将查询参数解析为映射。解析后的查询参数映射是从字符串到字符串切片的映射,因此如果只想获取第一个值,请使用`[0]`进行索引。
fmt.Println("Raw query:", u.RawQuery)
m, _ := url.ParseQuery(u.RawQuery)
fmt.Println(m)
}
// 代码的一部分来自:https://gobyexample.com/url-parsing
英文:
For parse urls use the golang net/url package: https://golang.org/pkg/net/url/
OBS: You should use the Authorization header for send auth tokens.
Example code with extracted data from your example url:
package main
import (
"fmt"
"net"
"net/url"
)
func main() {
// Your url with hash
s := "http://localhost:8080/#access_token=tokenhere&scope=scopeshere"
// Parse the URL and ensure there are no errors.
u, err := url.Parse(s)
if err != nil {
panic(err)
}
// ---> here is where you will get the url hash #
fmt.Println(u.Fragment)
fragments, _ := url.ParseQuery(u.Fragment)
fmt.Println("Fragments:", fragments)
if fragments["access_token"] != nil {
fmt.Println("Access token:", fragments["access_token"][0])
} else {
fmt.Println("Access token not found")
}
// ---> Others data get from URL:
fmt.Println("\n\nOther data:\n")
// Accessing the scheme is straightforward.
fmt.Println("Scheme:", u.Scheme)
// The `Host` contains both the hostname and the port,
// if present. Use `SplitHostPort` to extract them.
fmt.Println("Host:", u.Host)
host, port, _ := net.SplitHostPort(u.Host)
fmt.Println("Host without port:", host)
fmt.Println("Port:",port)
// To get query params in a string of `k=v` format,
// use `RawQuery`. You can also parse query params
// into a map. The parsed query param maps are from
// strings to slices of strings, so index into `[0]`
// if you only want the first value.
fmt.Println("Raw query:", u.RawQuery)
m, _ := url.ParseQuery(u.RawQuery)
fmt.Println(m)
}
// part of this code was get from: https://gobyexample.com/url-parsing
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论