http - 如何使用 golang http 客户端在 url 中正确设置路径参数?

标签 http post go

我正在使用 net/http 包,我想将动态值设置为 POST url:

http://myhost.com/v1/sellers/ {id}/随便

如何在此路径参数中设置 id 值?

最佳答案

您可以使用 path.Join构建网址。您可能还需要 pathEscape从外部接收的路径参数,以便它们可以安全地放置在路径中。

url1 := path.Join("http://myhost.com/v1/sellers", url.PathEscape(id), "whatever")
req, err := http.NewRequest(http.MethodPost, url1, body)
if err != nil {
    return err
}

关于http - 如何使用 golang http 客户端在 url 中正确设置路径参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43826412/

相关文章:

javascript - 如何从其他域获取ajax响应?

c# - 链接到本地​​目录中的图像与 URL,我做错了什么?

html - 无法将中文字符发布到我的搜索表单

mysql - WordPress 添加自定义列

go - 在 Go 中 defer 关键字是如何工作的

ssl - 在 http.NewRequest 检查服务器 SSL/TLS 证书的指纹

python - 如何在 Python 中提供 HTTP post 请求时在 Json 对象中传递变量

jquery - $.post/$.ajax 代码在超时后完成,但仍然执行来自服务器的调用

Golang Dockerfile : Unable to find packages in docker build but runs normally

javascript - CORS AJAX 请求到 Chrome 中本地 WSGI 服务器的延迟过高