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 :)

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

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

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

Hi Nguyễn Long,

please describe what POST request you want to make?

What functions should it do?

 
#8
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)
 
#9
People who like this:0Yes/0No
Just came
Rating: 0
Joined: Nov 22, 2014
Company:

Aleksandr Biryukov help me!

 
Subscribe
Users browsing this topic (guests: 1, registered: 0, hidden: 0)
Be the first to know tips & tricks 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.