How can I make a request with a bearer token in Go

前端 未结 3 1588
不思量自难忘°
不思量自难忘° 2021-02-12 16:44

I need to make a GET request to an API with a bearer token in the authorization request. How can I do this in Go? I have the following code, but I haven\'t had success.

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-12 16:57

    I made a super-little-basic library for execute basic request like:

    package main
    import (
         request "github.com/alessiosavi/Requests"
    )
    
    func main(){
        // Create a key-value list of headers
        headers := request.CreateHeaderList(`Accept`, `application/json`, "Authorization", "Bearer "+auth.IAMToken)
        resp :=request.SendRequest(`http://your_site.com`, `GET`, headers, nil))
    }
    

    Here you can find the request implementation:
    https://github.com/alessiosavi/Requests/blob/e7ca66bde738b6224fba2b6f146a8dbee67d3323/Requests.go

    Here you can find how i use the library for Bearer Auth and other auth type:
    https://github.com/alessiosavi/GoCloudant/blob/a8ad3a7990f04ea728bb327d6faea6af3e5455ca/cloudant.go

提交回复
热议问题