Desktop version

Main > Forum > 1C:Enterprise Platform > Studying 1C:Enterprise platform > Studying Http-Service

Forum

Search UsersRules
Studying Http-Service
#1
Just came
Points:: 0
Joined:: Nov 22, 2014

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

Profile
#2
Administrator
Points:: 0
Joined:: Oct 3, 2019

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 ...

Profile
#3
Just came
Points:: 0
Joined:: Nov 22, 2014

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.

Profile
#4
Administrator
Points:: 0
Joined:: Oct 3, 2019

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

Profile
Subscribe
Users browsing this topic (guests: 1, registered: 0, hidden: 0)



© 1C LLC. All rights reserved
1C Company respects the privacy of our customers and visitors
to our Web-site.