Get method in golang

huangapple go评论100阅读模式
英文:

Get method in golang

问题

我必须为大学做一份工作,事实上我无法完成,我需要通过GET请求从API请求数据,问题是API要求我登录才能使用该数据,我该如何在GO中进行身份验证?请帮帮我 Get method in golang

clienteHttp := &http.Client{}
url := "https://web.copiloto.ai/api/rawdata?vehicles=238&from=2021-05-20T00:00:00&to=2021-05-20T23:59:59&fields=$basic,ecu_bms1_total_voltage&distance=km"
peticion, err := http.NewRequest("GET", url, nil)
if err != nil {
    log.Fatalf("Error creando petición: %v", err)
}

peticion.Header.Add("Content-Type", "application/json")
peticion.Header.Add("X-Hola-Mundo", "Ejemplo")
respuesta, err := clienteHttp.Do(peticion)
if err != nil {
    log.Fatalf("Error haciendo petición: %v", err)
}

defer respuesta.Body.Close()

cuerpoRespuesta, err := ioutil.ReadAll(respuesta.Body)
if err != nil {
    log.Fatalf("Error leyendo respuesta: %v", err)
}

respuestaString := string(cuerpoRespuesta)
log.Printf("Código de respuesta: %d", respuesta.StatusCode)
log.Printf("Encabezados: '%q'", respuesta.Header)
contentType := respuesta.Header.Get("Content-Type")
log.Printf("El tipo de contenido: '%s'", contentType)
log.Printf("Cuerpo de respuesta del servidor: '%s'", respuestaString)

目前我只知道在浏览器中消耗数据时必须登录到页面,这样我才能获取数据,但是在浏览器之外无法实现。

更新:要登录,我必须通过POST方法发送用户名和密码,我有用户名和密码,我应该怎么做?
在Postman中测试时,我收到以下内容

{
    "message": "User successfully authenticated",
    "app": null,
    "auth": "4fe0e1733199bdaf1f7106aeea7754e6389ef1b7fa19e24a4"
}
英文:

I have to do a job for the university and the truth is that I cannot carry it out, I need to request data from an API through GET, the issue is that the API asks me to be logged in to consume said data, how can I perform this authentication from GO ?, please help Get method in golang

clienteHttp := &http.Client{}
url := "https://web.copiloto.ai/api/rawdata?vehicles=238&from=2021-05-20T00:00:00&to=2021-05-20T23:59:59&fields=$basic,ecu_bms1_total_voltage&distance=km"
peticion, err := http.NewRequest("GET", url, nil)
if err != nil {
	log.Fatalf("Error creando petición: %v", err)

}

peticion.Header.Add("Content-Type", "application/json")
peticion.Header.Add("X-Hola-Mundo", "Ejemplo")
respuesta, err := clienteHttp.Do(peticion)
if err != nil {
	
	log.Fatalf("Error haciendo petición: %v", err)
}

defer respuesta.Body.Close()

cuerpoRespuesta, err := ioutil.ReadAll(respuesta.Body)
if err != nil {
	log.Fatalf("Error leyendo respuesta: %v", err)
}

respuestaString := string(cuerpoRespuesta)
log.Printf("Código de respuesta: %d", respuesta.StatusCode)
log.Printf("Encabezados: '%q'", respuesta.Header)
contentType := respuesta.Header.Get("Content-Type")
log.Printf("El tipo de contenido: '%s'", contentType)
log.Printf("Cuerpo de respuesta del servidor: '%s'", respuestaString)

resp :

2021/05/25 18:12:26 Cuerpo de respuesta del servidor: '{"message": "User is NOT Logged in | Authenticated"}

At the moment I only know that to consume data from the browser I must be logged into the page, in this way I can obtain the data, but nothing outside the browser

upgrade: To log in I have to send the username and password through the POST method, I have the username and password, what should I do?
when testing in postman I receive the following

{
"message": "User successfully authenticated",
"app": null,
"auth": "4fe0e1733199bdaf1f7106aeea7754e6389ef1b7fa19e24a4"}

答案1

得分: 1

使用用户名和密码,您可以使用HTTP基本身份验证。

peticion.SetBasicAuth("user1", "passwd1!")

需要记住的是,基本身份验证的凭据是未加密的。如果您将它们添加到您的代码中,将来的开发人员将能够看到它们。如果您需要保护这些凭据,您需要将程序的输入或类似的内容进行处理,以便它们不仅仅是存在于代码中。

参考链接:https://golang.org/pkg/net/http/#Request.SetBasicAuth

英文:

With a user name and password, you might be able to make use of HTTP Basic Auth.

peticion.SetBasicAuth("user1", "passwd1!")

The thing to remember with Basic Auth is that your credentials are not encrypted. And if you add them to your code, they will be there for future developers to see. If you need to protect them, you will need to make the inputs to the program or something like that so that they are not just sitting in code.

For reference: https://golang.org/pkg/net/http/#Request.SetBasicAuth

huangapple
  • 本文由 发表于 2021年5月26日 06:20:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/67696263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定