简体   繁体   中英

how to pass dynamic parameter in the REST API url using golang

I am getting the encoded url from postman (google api). In the url, I have the query written to fetch info from a db. 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. But in no way I am able to pass this dynamic variable in the 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:

"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:

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

You can use the time package to create your date programmatically and format it for your query.

To do that, you can use 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 .

t := time.Now()
fmt.Println(t.Format("2006-01-02 15:04:05"))

Will output

2009-11-10 23:00:00

See the documentation of the time library

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM