1

I'm trying to pass parameters in the request body, the documentation says :

The request to be sent was defined in a prior call to the Open method. The calling application can provide data to be sent to the server through the Body parameter. If the HTTP verb of the object's Open is "GET", this method sends the request without Body, even if it is provided by the calling application.

So, I need to use POST with body. But when I use use POST with body I have error "Bad Request: message text is empty" and when I use GET with body result is ok. Why?

My code:

WinHttp = NEW COMObject("WinHttp.WinHttpRequest.5.1"); 
WinHttp.Open("GET", "http://api.telegram.org/botbotname/sendMessage", 0);

WinHttp.setRequestHeader("Content-type", "application/json");

JSONWr = New JSONWriter();  
JSONWr.ValidateStructure = False;   

JSONParams = New JSONWriterSettings( , Chars.Tab);  

JSONWr.SetString(JSONParams);   

JSONWr.WriteStartObject();

JSONWr.WritePropertyName("chat_id");
JSONWr.WriteValue(UserId);

JSONWr.WritePropertyName("text");
JSONWr.WriteValue(Text);

JSONWr.WriteEndObject();

JSONString = JSONWr.Close();

WinHttp.Send(JSONString);

work, but how? And why the same with POST doesn`t work?

  • What language are you using and why are you using `WinHttp`? All languages have native ways of using HTTP. Even for scripting, PowerShell has the `Invoke-WebRequest` and `Invoke-RestMethod` classes. What error do you get? BTW `GET` can't send a body, so your `GET` isn't working well either – Panagiotis Kanavos Apr 19 '21 at 08:47
  • It will be too difficult to explain why I use WinHttp, but I need this solution. In my case is two params "chat_id" and "text". In both case (1. send params in url string 2. send params in json as in description of my question) it works with GET method. But when I use post I have error "Bad Request: message text is empty" – Ghost Programmer Apr 19 '21 at 08:58
  • Post your actual code. It will be a lot more difficult to find a solution if you force people to just guess. GET doesn't have a body. There's no if or but or maybe about this. The remote server is probably ignoring the body. For POST though, the error clearly says that the body is bad, and even explains that the `message text is empty`. Which means `someJsonString` is invalid – Panagiotis Kanavos Apr 19 '21 at 09:11
  • Just because it is not common language "1с". Updeted question, added more code – Ghost Programmer Apr 19 '21 at 09:37
  • Looking at the company's site I see they have [their own way of making HTTP requests](https://1c-dn.com/blog/work-with-http-services-in-1c-part-2-post-method/?sphrase_id=168177). Have you tried it? If the calls you make to Telegram are wrong, changing the client library won't fix anything – Panagiotis Kanavos Apr 19 '21 at 09:47
  • 1
    Have you tried making a call to Telegram through PostMan or any other tool? – Panagiotis Kanavos Apr 19 '21 at 09:48
  • Of course, I checked PostMan, everything is correct. I can do it in other ways, but I want to understand what I am doing wrong and how to do it correctly with WinHttp – Ghost Programmer Apr 19 '21 at 09:58
  • 1
    Again, it's not WinHTTP. The request is wrong. You aren't even using WinHTTP, the request is created by JSONWriter, a 1c class. Have you actually checked what it produces? Does that string work if you use it with Postman? Have you tried using Fiddler or another debugging proxy to see what's really sent to the server? And once again, why don't you use 1c's own classes? If the JSON string is wrong, no library will work – Panagiotis Kanavos Apr 19 '21 at 10:01
  • The string is correct, this string work with Postman and work with WinHTTP using GET, and that is strange – Ghost Programmer Apr 19 '21 at 10:59
  • @PanagiotisKanavos, I used HttpDebugger, body of my request is correct. Have no idea what to try – Ghost Programmer Apr 22 '21 at 12:31

0 Answers0