Desktop version

Main > Forum > 1C:Enterprise Platform > 1C:Integration > HTTP Services and JSON

Forum

Search UsersRules
HTTP Services and JSON
#1
Active user
Points:: 0
Joined:: Feb 26, 2015

Hi
I need an example. (HTTP Service with JSON)

I could not find in the website.

Please could you share an example?

Profile
#2
Guest
Points::
Joined::

Hello, Erdogan!

Here is an example on how to create a HTTP service that returns data from the Items catalog.
You need to do following:

  • Create a HTTP service, remember to set the Root URL, for example same as the name of the service.
  • Add an URL template, where specify template for parameters, for example: /item/{ItemCode}/ that means that instead of {ItemCode} one should pass a value of ItemCode parameter.
  • Add a method with the get name and a handler for it.
  • Publish HTTP services.
  • Use an URL of following structure to get data: http://<Infobase publication path>/hs/<HTTP service root URL><HTTP service template>, for example:
Code
http://localhost/Tests1cdn/hs/JSON/item/0000001/


Here is an example of the get method that finds a catalog item by its code and returns JSON-serialized value:
Code
Function ItemGet(Request)
   
   Result = "";
   ResultSuccessful = False;
   
   If Request.URLParameters.Get("ItemCode") <> Undefined Then
      ItemCode = Request.URLParameters.Get("ItemCode");
      
      JSONWriter = New JSONWriter;
      JSONWriterSettings = New JSONWriterSettings(, Chars.Tab);
      JSONWriter.SetString(JSONWriterSettings);
      
      Ref = Catalogs.Items.FindByCode(ItemCode);
      If ValueIsFilled(Ref) Then
         Item = Ref.GetObject();
      
         XDTOSerializer.WriteJSON(JSONWriter, Item, XMLTypeAssignment.Explicit);
         Result = JSONWriter.Close();
         ResultSuccessful = True;
      Else
         Result = StrTemplate(NStr("en = 'The Items catalog does not contain an item with %1 code.'"), ItemCode);
      EndIf;
   Else
      Result = NStr("en = 'Please, specify the item code.'");
   EndIf;
   
   Response = New HTTPServiceResponse(?(ResultSuccessful, 200, 404));
   Response.SetBodyFromString(Result);
   Return Response;
   
EndFunction

Profile
#3
Guest
Points::
Joined::

You can also serialize a structure and receive more common JSON structure:

Code
Function ItemGet(Request)
   
   Result = "";
   ResultSuccessful = False;
   
   If Request.URLParameters.Get("ItemCode") <> Undefined Then
      ItemCode = Request.URLParameters.Get("ItemCode");
      
      JSONWriter = New JSONWriter;
      JSONWriterSettings = New JSONWriterSettings(, Chars.Tab);
      JSONWriter.SetString(JSONWriterSettings);
      
      Ref = Catalogs.Items.FindByCode(ItemCode);
      If ValueIsFilled(Ref) Then
         Item = Ref.GetObject();
         
         ItemStructure = New Structure;
         ItemStructure.Insert("Code", Item.Code);
         ItemStructure.Insert("Description", Item.Description);
         WriteJSON(JSONWriter, ItemStructure);
         Result = JSONWriter.Close();
         ResultSuccessful = True;
      Else
         Result = StrTemplate(NStr("en = 'The Items catalog does not contain an item with %1 code.'"), ItemCode);
      EndIf;
   Else
      Result = NStr("en = 'Please, specify the item code.'");
   EndIf;
   
   Response = New HTTPServiceResponse(?(ResultSuccessful, 200, 404));
   Response.SetBodyFromString(Result);
   Return Response;
   
EndFunction

Profile
#4
Guest
Points::
Joined::

To read data from third-party HTTP service that uses JSON, you can use HTTPConnection object.

Here I made an example that generates and receives JSON objects over HTTP service. See the attached configuration.

Code
&AtClient
Procedure GetDescription(Command)
   
   ResponseFile = GetTempFileName("json");
   ServerName = StrReplace(InfobaseURL, "http://", "");
   ServerName = Left(ServerName, Find(ServerName, "/")-1);
   Path = StrReplace(InfobaseURL, "http://" + ServerName, "") + "/hs/JSON/item/" + Code + "/";
   
   Try
      HTTP = New HTTPConnection(ServerName);
      HTTP.Get(Path, ResponseFile);
   Except
      Description = BriefErrorDescription(ErrorInfo());
      Return;
   EndTry;
   
   JSONReader = New JSONReader;
   JSONReader.OpenFile(ResponseFile);
   Data = ReadJSON(JSONReader);
   JSONReader.Close();
   
   Description = Data.Description;
   
   BeginDeletingFiles(, ResponseFile);
   
EndProcedure

Profile
#5
Active user
Points:: 0
Joined:: Feb 26, 2015

Super :)

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

Hi Timofey Bugaevsky!
Thank you for a very good example, how do I want to use the Post method?
Can you make an example for me please

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

Hi Nguyễn Long,

please describe what POST request you want to make?

What functions should it do?

Profile
#8
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
#9
Just came
Points:: 0
Joined:: Nov 22, 2014

Aleksandr Biryukov help me!

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



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