rest service

The 1C:Enterprise developers forum

#1
People who like this:0Yes/0No
Just came
Rating: 1
Joined: May 8, 2018
Company: Profsis Yazılım Ltd. Şti.

Hello there
I want to contact a rest service from program 1c.
Is there an example in the program 1c for this?
I want to contact http://webservice.tsoft.com.tr
http://webservice.tsoft.com.tr/rest1/console

Edited: harun Fişekci - Oct 26, 2019 10:46 AM
 
#2
People who like this:0Yes/0No
Administrator
Rating: 9
Joined: Oct 3, 2019
Company:

Hello Harun Fişekci!

Of course, you can work with REST services - the 1C platform is perfect for this!

You may to use the HTTPConnection object. For example, it may look like a code to access your service:



Code
Token = "YourToken";

ServerName = "webservice.tsoft.com.tr";
URL = "/rest1/brand/getBrands";
   
Headers = New Map;
   
Headers.Insert("token", Token);
Headers.Insert("Content-Type", "application/x-www-form-urlencoded");
Headers.Insert("FetchShowcase", True);
      
HttpQuery = New HTTPRequest(URL, Headers);
  
HttpConnection = New HTTPConnection(ServerName, 443,,,,, New OpenSSLSecureConnection);
   
HttpAnswer = HttpConnection.Post(HttpQuery, "");
   
If HttpAnswer.StatusCode = 200 Then 
      
    BodyAnswer = HttpAnswer.GetBodyAsString(TextEncoding.UTF8);
      
EndIf;




Unfortunately, I don`t have a token, so I get this answer: "Token bulunamad".


If you have any questions, I`ll be glad to answer them.

 
#3
People who like this:0Yes/0No
Administrator
Rating: 9
Joined: Oct 3, 2019
Company:

Hi Harun Fişekci!

I`ll see it and write the result.

 
#4
People who like this:1Yes/0No
Administrator
Rating: 9
Joined: Oct 3, 2019
Company:

Hi Harun Fişekci!

Please send me a new token.

Just send to my mail only: abir@1c.com

 
#5
People who like this:0Yes/0No
Just came
Rating: 1
Joined: May 8, 2018
Company: Profsis Yazılım Ltd. Şti.

harun@profsis.com

 
#6
People who like this:0Yes/0No
Administrator
Rating: 9
Joined: Oct 3, 2019
Company:

Hi Harun!

I understood the reason for the failure.

This code will work with your service:

Code
Procedure Command1(Command)
   
   Token = "YourToken";
   
   stringToken = "token=" + Token;

   ServerName = "www.atef.com.tr";
   URL = "/rest1/brand/getBrands";
      
   Headers = New Map;
      
   // Headers.Insert("token", Token); We remove this line ...
   
   Headers.Insert("Content-Type", "application/x-www-form-urlencoded");
   Headers.Insert("FetchShowcase", True);
         
   HttpQuery = New HTTPRequest(URL, Headers);
   
   
   HttpQuery.SetBodyFromString(stringToken); // ... and add this line
     
   
   HttpConnection = New HTTPConnection(ServerName, 443,,,,, New OpenSSLSecureConnection);
      
   HttpAnswer = HttpConnection.Post(HttpQuery, "");
      
   BodyAnswer = "";
   
   If HttpAnswer.StatusCode = 200 Then 
       BodyAnswer = HttpAnswer.GetBodyAsString(TextEncoding.UTF8);
   EndIf;
   
   Message(BodyAnswer);
   
EndProcedure


After these changes everything works

 
#7
People who like this:0Yes/0No
Administrator
Rating: 9
Joined: Oct 3, 2019
Company:

By the way, there is another way to work with HTTP:


Code
&AtClient
Procedure Command1(Command)
   
   WinHttp = New COMObject("WinHttp.WinHttpRequest.5.1");
   
   WinHttp.Open("POST", "https://www.atef.com.tr/rest1/brand/getBrands", False);
   
   WinHttp.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
   
   WinHttp.Send("token=YourToken");
   
   If WinHttp.Status = 200 Then
      Message(WinHttp.ResponseText);
   Else 
      Message("HTTP " + WinHttp.Status + " " + WinHttp.StatusText);
      Return;
   EndIf;      
   
EndProcedure



But this method works in Windows only.

 
Subscribe