duan02468 2018-12-12 03:47
浏览 354
已采纳

如何在POST方法中传递标头?

Here I have a function in which I'm sending a POST request which is used to save customer in the squareup with data and also set the headers with Authentication using variable_name.Header.Set() But in body response it will always give me error of the:-

"errors":[
     {"category":"AUTHENTICATION_ERROR",
      "code":"UNAUTHORIZED",
      "detail":"Your request did not include an `Authorization` http header with an access token. }]}

But In the function I'm setting the authentication token.

Code:-

func CreateCustomer(c *gin.Context) {
customer := models.Customer{}
bearer := strings.Split(c.Request.Header["Authorization"][0], "Bearer")// token pass in the postman.
bearerToken := strings.TrimSpace(bearer[1])

customerErr := json.NewDecoder(c.Request.Body).Decode(&customer)
if customerErr != nil {
    fmt.Println(customerErr)
    return
}
fmt.Println(customer)
bindData, err := json.Marshal(customer)
if err != nil {
    panic(err)
}
var jsonStr = []byte(string(bindData))
url :="https://connect.squareup.com/v2/customers"
fmt.Println(url)

req, err := http.Post(url, "application/json", bytes.NewBuffer(jsonStr))
// I used this one too.
// req.Header.Set("Authorization", "Bearer "+bearerToken)
// req.Header.Set("Accept", "application/json")
req.Header.Add("Authorization", "Bearer "+bearerToken)
req.Header.Add("Accept", "application/json")
fmt.Println(req.Header)
if err != nil {
    panic(err)
}
defer req.Body.Close()
body, _ := ioutil.ReadAll(req.Body)
fmt.Println("response Body:", string(body))
}

type Customer struct {
  GivenName    string   `json:"given_name" bson:"given_name"`
  FamilyName   string   `json:"family_name" bson:"family_name"`
  CompanyName  string   `json:"company_name" bson:"company_name"`
  Nickname     string   `json:"nickname" bson:"nickname"`
  EmailAddress string   `json:"email_address" bson:"email_address"`
  Address      Addresss `json:"address" bson:"address"`
  PhoneNumber  string   `json:"phone_number" bson:"phone_number"`
  ReferenceId  string   `json:"reference_id" bson:"reference_id"`
  Note         string   `json:"note" bson:"note"`
}

The req.Header result is:-

map[X-Xss-Protection:[1; mode=block] 
    Keep-Alive:[timeout=60] 
    Accept:[application/json] 
    X-Permitted-Cross-Domain-Policies:[none] 
    Content-Type:[application/json] 
    Vary:[Origin, Accept-Encoding] 
    X-Content-Type-Options:[nosniff] 
    X-Download-Options:[noopen] 
    X-Frame-Options:[SAMEORIGIN] 
    Date:[Wed, 12 Dec 2018 03:41:16 GMT] 
    Strict-Transport-Security:[max-age=631152000] 
    Authorization:[Bearer YOUR_TOKEN HERE]]

Can anyone tell me that what error should I'm doing or where I do correction that it will able to save customer in the Squareup?

  • 写回答

1条回答 默认 最新

  • dtry54612 2018-12-12 04:00
    关注

    Your code sends POST request and after request is processed it adds headers to response struct:

    response, err := http.Post(url, "application/json", bytes.NewBuffer(jsonStr))

    You should set headers first and send request after that:

    // create request
    req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
    if err != nil {
       panic(err)
    }
    
    // set headers
    req.Header.Add("Authorization", "Bearer "+bearerToken)
    req.Header.Add("Accept", "application/json")
    
    // send request with headers
    client := &http.Client{}
    response, err := client.Do(req)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据