使用Golang和Telegram API时出现400 Bad Request错误。

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

400 Bad Request with Golang and Telegram API

问题

我正在构建一个使用给定的机器人令牌向 Telegram 频道执行 POST 请求的 Golang 应用程序,但是当我这样做时,我得到了一个 400 Bad Request 的错误。

这是我的 POST 请求代码:

import (
  "fmt"
  "net/url"
  "net/http"
  "strings"
)

. . .

request_url := "https://api.telegram.org/bot{token}/sendMessage?chat_id={channelId}"

urlData := url.Values{}
urlData.Set("text", "Hello!")

client := &http.Client{}
req, _ := http.NewRequest("POST", request_url, strings.NewReader(urlData.Encode()))
req.Header.Set("content-type", "application-json")
res, err := client.Do(req)
if err != nil {
  fmt.Println(err)
} else {
  fmt.Println(res.Status)
}

我不明白为什么会返回 400 错误,尽管我可以使用 Postman 执行相同的 POST 请求:

POST https://api.telegram.org/bot{token}/sendMessage?chat_id={channelId}
body: {"text": "Hello"} Content-Type=application/json

有什么提示可以解决这个问题吗?

我已经思考了很长时间,但是还没有解决。

英文:

I'm building a golang application which performs a POST to a Telegram Channel using a given Bot token but when I do it I get

> 400 Bad Request

This is my POST:

import (
  "fmt"
  "net/url"
  "net/http"
  "strings"
)

. . .

  request_url := "https://api.telegram.org/bot{token}/sendMessage?chat_id={channelId}"

  urlData := url.Values{}
  urlData.Set("text", "Hello!")

  client := &http.Client{}
  req, _ := http.NewRequest("POST", request_url, strings.NewReader(urlData.Encode()))
  req.Header.Set("content-type", "application-json")
  res, err := client.Do(req)
  if(err != nil){
	  fmt.Println(err)
  } else {
	  fmt.Println(res.Status)
  }

I don't get why it's giving me 400 even thought I'm able to perform the very same POST using Postman

POST https://api.telegram.org/bot{token}/sendMessage?chat_id={channelId}
body : {"text" : "Hello"} Content-Type=application/json

Any Hint on how to fix this?

I've been scratching my head for a while but I wasn't able to solve this.

UPDATE

Trying @old_mountain approach leads to the same result

import (
    "fmt"
    "net/http"
    "bytes"
    "encoding/json"
)

  request_url := "https://api.telegram.org/bot{token}/sendMessage?chat_id={channelId}"

  client := &http.Client{}
  values := map[string]string{"text": "Hello!"}
  jsonStr, _ := json.Marshal(values)
  req, _ := http.NewRequest("POST", request_url, bytes.NewBuffer(jsonStr))
  req.Header.Set("content-type", "application-json")

  res, err := client.Do(req)
  if(err != nil){
	  fmt.Println(err)
  } else {
	  fmt.Println(res.Status)
  }

答案1

得分: 3

你需要发送一个 JSON 字符串。

var jsonStr = []byte(`{"text":"Hello!"}`)
req, _ := http.NewRequest("POST", request_url, bytes.NewBuffer(jsonStr))

或者,如果你不想直接编写它:

values := map[string]string{"text": "Hello!"}
jsonStr, _ := json.Marshal(values)
req, _ := http.NewRequest("POST", request_url, bytes.NewBuffer(jsonStr))

此外,调整头部的 Content-Type 为:

req.Header.Set("Content-Type", "application/json")
英文:

You need to send a json string.

var jsonStr = []byte(`{"text":"Hello!"}`)
req, _ := http.NewRequest("POST", request_url, bytes.NewBuffer(jsonStr))

Or, if you don't want to directly write it:

values := map[string]string{"text": "Hello!"}
jsonStr, _ := json.Marshal(values)
req, _ := http.NewRequest("POST", request_url, bytes.NewBuffer(jsonStr))

Also, adjust the header Content-Type to:

req.Header.Set("Content-Type", "application/json")

huangapple
  • 本文由 发表于 2017年3月8日 22:11:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/42673582.html
匿名

发表评论

匿名网友

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

确定