Studying Http-Service

Understanding basics of 1C:Enterprise platform. To start working with 1C:Enterprise platform visit Getting started page

#1
People who like this:0Yes/0No
Just came
Rating: 0
Joined: Nov 22, 2014
Company:

Hi Aleksey Bochkov. I am studying Http-Service on 1C, do you have any documents or examples for me please.

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

Hi Nguyễn Long!

Please describe which HTTP-service you want to do. What should it do? What data should it give?

I'll try to make a small example for you ...

 
#3
People who like this:0Yes/0No
Just came
Rating: 0
Joined: Nov 22, 2014
Company:

I want to use HTTP -Services to receive data by Post method from a website or from a DataProcessor.
When HTTP -Services receives the data after the post method in JSON format, it will save the data into the database and return the saved data to it.
When you press the button to save the entire dose to HTTP -Services by POST method, in HTTP -Services will process the data and save it into PostsSON Catalogs, then return the result,
I want you to help me perfect that small idea.

Download 1Cv8.cf (19.96 KB)
 
#4
People who like this:0Yes/0No
Administrator
Rating: 5
Joined: Oct 3, 2019
Company:

Hi Nguyễn Long!

First you need to make a HTTP service in your server system (see screenshot 1).

Then this service must be published on the web server (screenshot 2).

After the service is published, it can be accessed using the POST request. Here is an example of 1C code for accessing a previously published HTTP service:



Code
   structureToServer = New Structure;
   
   structureToServer.Insert("email",    email);
   structureToServer.Insert("fullname",    fullname);
   
   JSONWriter = New JSONWriter;
   
   JSONWriter.SetString();
   WriteJSON(JSONWriter,structureToServer);
   
   stringForRequest = JSONWriter.Close();
   
   stringConnectionString = "Path to your server";
   
   HTTPConnection = New HTTPConnection(stringConnectionString);
    
   HTTPRequest = New HTTPRequest("YourServer/hs/ClientAPI/V1/ClientAPI");
    
   HTTPRequest.SetBodyFromString(stringForRequest);
   
   HTTPAnswer = HTTPConnection.Post(HTTPRequest);
   
   If HTTPAnswer.StatusCode = 200 Then 
      Result = HTTPAnswer.GetBodyAsString();
   EndIf;




The structure is created in the code. This structure has "email" and "fullname" fields. Then this structure is converted to the JSON format and transmitted to the server.

Then we get the response from the server (HTTPAnswer) and also convert it to the String (Result).



And this is how the data processing on the server will look:


Code
Function ClientAPIPOST_V1(Request)
   
   stringFromClient = Request.GetBodyAsString("UTF-8");
   
   JSONReader = New JSONReader;
   JSONReader.SetString(stringFromClient);
   
   structureFromClient = ReadJSON(JSONReader);
   JSONReader.Close();
   
   If structureFromClient.Property("fullname")
      And ValueIsFilled(structureFromClient.fullname) Then
      
      // Here you can write your code for data processing, 
      // for example, create a new document or make an entry in the Catalog
      
      Response = New HTTPServiceResponse(200);
      stringResponse = "Fullname field isn't empty";
      
      Response.SetBodyFromString(stringResponse);
      
   Else 
      
      Response = New HTTPServiceResponse(204); // 204 No Content
      stringResponse = "Fullname field is empty";
      
      Response.SetBodyFromString(stringResponse);
      
   EndIf;
   
   Return Response;
   
EndFunction

Download 1.png (8.43 KB)
Download 2.png (60.59 KB)
 
Subscribe
Be the first to know tips & trick on business application development!

A confirmation e-mail has been sent to the e-mail address you provided .

Click the link in the e-mail to confirm and activate the subscription.