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)
}