简体   繁体   English

如何使用golang在REST API url中传递动态参数

[英]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

See the documentation of the time library查看时间库的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用WSO2 ESB代理服务时,如何在REST服务的URL中传递动态参数? - How do I pass a dynamic parameter as a part of the URL of a REST service while using WSO2 ESB Proxy Service? 如何通过 rest api ZE6B391A8D2C4D55902A23A8B 传递 url - How to pass an url by a rest api URL string 如何将参数传递到数组 JSON Rest API - How to pass parameter into array JSON Rest API REST API-在哪里传递ID参数? 在网址或标题中 - REST API - where to pass ID parameter? in URL or in header 如何通过构建函数将GitHub的URL传递给Docker的REST API远程参数 - How to pass GitHub's URL to Docker's REST API remote parameter from build function 如何使用 Springboot 在 REST 请求 URL 中将 json 对象作为参数传递 - How to pass a json object as a parameter in a REST request URL with Springboot 如何在 FLASK 中将 url 作为 REST GET 参数传递 - How do you pass a url as a REST GET parameter in FLASK 带空格的 URL rest api 参数 - URL rest api parameter with spaces 我如何在REST API中将带有斜线的参数作为PathParam传递 - how can I pass parameter with slash as the PathParam in rest API 如何在Spring Rest API中将字节数组作为参数传递 - How to pass byte array as a parameter in Spring Rest API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM