英文:
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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论