[英]how to pass dynamic parameter in the REST API url using golang
I am getting the encoded url from postman (google api).我从邮递员(google api)获取编码的 url。 In the url, I have the query written to fetch info from a db.
在 url 中,我编写了从数据库中获取信息的查询。 In the query, I have hardcoded the date parameter for which I want to fetch the data.
在查询中,我对要获取数据的日期参数进行了硬编码。 But when I am using the encoded query in my code (written in golang), I want the date parameter to be coming from a variable which should be changing on daily basis.
但是当我在我的代码中使用编码查询(用 golang 编写)时,我希望日期参数来自一个应该每天都在变化的变量。 But in no way I am able to pass this dynamic variable in the url.
但是我无论如何都无法在 url 中传递这个动态变量。 Any help on this?
有什么帮助吗?
Code snippet:代码片段:
https://*************/?q='Last Date' >= "09/04/2018 12:00:00 AM " and 'Last Date' <= "10/04/2018 11:59:59 PM"&fields=values(ID, Name)
This is the url I'm using in postman and I am getting an encoded url in return something like this:这是我在邮递员中使用的 url,我得到一个编码的 url 作为回报,如下所示:
"https://*************************/?q=Last%20Date'%20%3E%3D%20%2209%2F04%2F2018%2012%3A00%3A00%20AM%20%22%20and%20'Last%20Date'%20%3C%3D%20%2210%2F04%2F2018%2011%3A59%3A59%20PM%22&fields=values(ID%2CName)
In place of Last Date (which is hard coded in url), I am trying to use a variable which has the dates coming from somewhere else (dynamic variable), something like this:代替最后日期(在 url 中硬编码),我试图使用一个变量,它的日期来自其他地方(动态变量),如下所示:
i:= RunDate.AddDate(0, 0, 1)
I want to add 'i' in place of Last Date in the query, but by doing so, it's throwing error我想在查询中添加 'i' 代替 Last Date,但这样做会引发错误
You can use the time
package to create your date programmatically and format it for your query.您可以使用
time
包以编程方式创建日期并为查询设置格式。
To do that, you can use time.Format()
.为此,您可以使用
time.Format()
。 For example to get your time in the format of yyyy-MM-dd HH:mm:ss
you can use the format string "2006-01-02 15:04:05" as the parameter to time.Format
.例如,让您的时间,格式
yyyy-MM-dd HH:mm:ss
,你可以使用格式字符串“2006-01-02 15点04分05秒”作为参数传递给time.Format
。
t := time.Now()
fmt.Println(t.Format("2006-01-02 15:04:05"))
Will output会输出
2009-11-10 23:00:00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.