Work with HTTP services in 1C. Part 1: GET method

Alexander Biryukov

15.04.2020 22 min

 «If your business is not on the Internet, then your business will be out of business.»

Bill Gates

 This rather old phrase has recently acquired quite tangible meaning because the question of the survival of many companies is now based on the conclusion of operations on the Internet. And when accessing the Internet, integration of the corporate system with third-party applications, a corporate website, and various online services will be crucial.

 The 1C platform provides several ways to interact with web services:

HTTP services is an exciting technology that allows you to solve many problems with minimal cost. HTTP services are easy to program - you write, in fact, a minimum of code. All queries received over HTTP, the specification of which has specific methods. A total of 16 methods - they cover all the typical operations that are required when developing such web applications.

And, most importantly, HTTP services are ready for integration with other web applications. This is probably their main feature.

When should we use HTTP services in the 1C:Enterprise platform?

  • Micro-exchanges between different information databases are more comfortable and more efficient to implement through HTTP services.
  • Through HTTP services, it is very convenient to upload information in a format such as JSON. This can be implemented very quickly, and we have several solutions that use this particular pattern.

  • One of the ideal ways to use HTTP services is to implement a full-fledged Backend for web applications.

The 1C platform allows you to create both client applications and server applications. And the platform's tools allow you to work with any HTTP services.

We'll create an HTTP service based on the 1C platform, and then try to connect to this service from another 1C program.

 Before we start, you must have a configured webserver to complete the creation of this application. You also need to be able to publish your 1C program on a web server. How to do this is described here: https://1c-dn.com/library/publishing_1c_configuration_to_web_on_windows/ 

 Let's go!

 So what is HTTP service? This is an object in the 1C platform, with which you can access the 1C program from the outside. We determine the logic of this object by ourselves, by writing program code - well, everything is as usual.

 To access the HTTP service from outside, a special URL is used. Let's first understand what it is made up of.

 So, the URL template is as follows:

 http://host/base/hs/rootURL/relativeURL?Parameters

 "host" - IP address or domain name of the webserver.

 "base" - The name of the 1C program indicated upon publication.

 "hs" - is a sign that an HTTP service is being accessed.

 "rootURL" - is the name of the resource that defines the group of resources associated with a universal meaning. It is set in the property of the object HTTP service.

 "relativeURL" - defines the resource that will be accessed. The relative URL specified in the request will be used to determine the specific resource that was accessed. The matching rule is specified in the "URL Templates" object.

 “Parameters” - optional parameters are passed after "?" sign, most often additional parameters or selection parameters, for example, response format (format = json or format = atom).

 Well - creates an HTTP service and immediately set the "rootURL" for this:

 1

Create a URL Template:

 2

Our template looks like this: /V1/{MethodName}, where {MethodName} is a parameter.

It is this parameter that we will pass from the outside, and 1C will process it.

Add the GET method:

3

Now, by clicking on the "Handler" button, we automatically generate the program code:

4

As you can see, 1C platform has formed a template that always returns a ResponseStatus = 200.

In general, now you can check the operation of this HTTP service, but for fun, let's add something to the program code.

For example, let's make the HTTP service return just a line of text:

Function ourAPIGET(Request)

    Response = New HTTPServiceResponse(200);

    result = "1C works fine!";

    Response.SetBodyFromString(result);

    Return Response;

EndFunction

Then publish the HTTP service on the webserver:

 5

Let's check the result:

6

Everything works!

Please note that as the parameter, we passed just the text "someparameter". Let's complicate our program code so that our HTTP service returns data depending on the passed "parameter".

Let's create a new catalog "Products" (without additional attributes) and fill it with several items:

7 


8

 

Change the source code of our HTTP service as follows:

Function ourAPIGET(Request)

      Response = New HTTPServiceResponse(200);

      MethodName = Request.URLParameters.Get("MethodName");

      If MethodName = "list" Then

            result = GetProducts();

      ElsIf MethodName = "product" Then

            Parameter = Request.QueryOptions.Get("code");

            If parameter = Undefined Then

                  Response.StatusCode = 405;

                  result = "Parameter not defined";

            Else

                  result = GetProducts(Parameter);

            EndIf;

      Else

            Response.StatusCode = 405;

            result = "Not found Method: " + MethodName;

      EndIf;

      Response.SetBodyFromString(result,TextEncoding.UTF8);

      Response.Headers.Insert("Content-Type","text/html; charset=utf-8");

      Return Response;

EndFunction

 

Function GetProducts(code = Undefined)

      result = "Product    Code<br><br>";

      query = New Query;

       If code = Undefined Then

            query.Text = "SELECT

                         |    Products.Presentation AS Presentation,

                         |    Products.Code AS Code

                         |FROM

                         |    Catalog.Products AS Products

                         |

                         |ORDER BY

                         |    Code";

      Else

            query.Text = "SELECT

                         |    Products.Presentation AS Presentation,

                         |    Products.Code AS Code

                         |FROM

                         |    Catalog.Products AS Products

                         |WHERE

                         |    Products.Code = &Code";

              query.SetParameter("Code", code);

      EndIf;

      selection = query.Execute().Select();

      While selection.Next() Do

            result = result + selection.Presentation + "    " + selection.Code + "<br>";

      EndDo;

      Return result;

EndFunction // GetItemByCode


Now HTTP service processes two methods: "list" and "product". The "list" method returns a list of all products from the catalog "Products". If the "product" method is processed, then the HTTP service additionally expects the "code" parameter - this is the Item code from the catalog "Products". Accordingly, in this case, the HTTP service returns one specific product.

If the method could not be determined, then the status 405 is returned.

Well, let's check how it works. Once again, we update the publication of 1C on the webserver (for fidelity, you can also restart the webserver) and again open the address in the browser: http://localhost/ourAPI/hs/ourAPI/V1/someparameter.

As we expected, in response, we see error 405:

 

9

 

Let's now open the correct address in the browser, for example http://localhost/ourAPI/hs/ourAPI/V1/list.

And what did we get there? That's what:

10

And now open this link: http://localhost/ourAPI/hs/ourAPI/V1/product?Code=000000002

Here is the result:

11

As you can see, everything is effortless.

I want to note that not only your browser but any program that can work with REST API can receive data from the HTTP service.

Ok, but now suppose we need to not only receive data from the server but also post data to the server too. The HTTP service that we just did isn't very suitable for this since it implements the method "GET".

We need an implementation of the method "POST". The 1C platform allows us to create such HTTP services. In the second part of the article, we'll be engaged in the creation of such a service.


You can download this Example for your own application.

If you have any questions about this article, you can always get answers on our forum: https://1c-dn.com/forum/


Stay with us.

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.