HTTP Services and JSON

1C:Enterprise platform integration capabilities and techniques

#1
People who like this:0Yes/0No
Active user
Rating: 4
Joined: Feb 26, 2015
Company: Uguz Limited sirketi

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

I could not find in the website.

Please could you share an example?

 
#2
People who like this:0Yes/0No
Timofey Bugaevsky
Guest

Joined:
Company:

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

 
#3
People who like this:0Yes/0No
Timofey Bugaevsky
Guest

Joined:
Company:

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

 
#4
People who like this:0Yes/0No
Timofey Bugaevsky
Guest

Joined:
Company:

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

Download json.cf (10.58 KB)
 
#5
People who like this:0Yes/0No
Active user
Rating: 4
Joined: Feb 26, 2015
Company: Uguz Limited sirketi

Super :)

 
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.