NOTE: This page has been translated automatically from Russian to English. Original page.



The HOWTO: Create and debug HTTP-service in the 1C: Enterprise

An article about how you can quickly create an HTTP-service in the 1C: Enterprise and how to implement it debugging.

Let's start from the end: in the end should turn out ...

Creating HTTP-service.

Suppose that we need a HTTP-service is available on request will return a list of users.

Look it should as follows: http: // the localhost / base / hs / = getUsers the info the action?

Return must JSON string (an array of JSON objects with properties: user name, user id):

[ { "name": "Администратор", "id": "563e7509-83bb-11e5-80c3-00505601174a" }, { "name": "Гость", "id": "423a9d3d-88fc-11e5-80c3-00505601174a" } ] -11e5-80c3-00505601174a"}, [ { "name": "Администратор", "id": "563e7509-83bb-11e5-80c3-00505601174a" }, { "name": "Гость", "id": "423a9d3d-88fc-11e5-80c3-00505601174a" } ] -88fc-11e5-80c3-00505601174a"} [ { "name": "Администратор", "id": "563e7509-83bb-11e5-80c3-00505601174a" }, { "name": "Гость", "id": "423a9d3d-88fc-11e5-80c3-00505601174a" } ] 

To implement it, create a configuration (or expansion) of the object-the HTTP service. As it will be called - does not matter, for the sake of simplicity let's call it "info". The root URL must be set to «info».

HTTP-service creation

Next, add to it a URL pattern for simplicity, let's call him "master". The value of the template should be equal to "/ *".

URL patterns

By the pattern we add the GET method: name = «GET», HTTP-method = «GET».

Method adding

Note. The method name are encouraged to nominate for the HTTP-method name.

Create a handler for GET HTTP-method.

Handle setting

Default method handler procedure is filled with code that returns a successful standard HTTP response (code 200).

Функция ОсновнойGET(Запрос) Ответ = Новый HTTPСервисОтвет(200); Возврат Ответ; КонецФункции 

We correct it so that it returns us to the list of users.

Функция ОсновнойGET(Запрос) Ответ = Новый HTTPСервисОтвет(200); Если НРЕГ(Запрос.ПараметрыЗапроса.Получить("action")) = "getusers" Тогда Ответ.УстановитьТелоИзСтроки(ВернутьСписокПользователей()); КонецЕсли; Возврат Ответ; КонецФункции Функция ВернутьСписокПользователей() Запрос = Новый Запрос; Запрос.Текст = "ВЫБРАТЬ | Пользователи.Ссылка |ИЗ | Справочник.Пользователи КАК Пользователи |ГДЕ | Пользователи.Недействителен = ЛОЖЬ"; Выборка = Запрос.Выполнить().Выбрать(); ОтветМассив = Новый Массив; Пока Выборка.Следующий() Цикл ОтветМассив.Добавить(Новый Структура("name,id",Выборка.Ссылка.Наименование,""+Выборка.Ссылка.УникальныйИдентификатор())); КонецЦикла; Ответ = Новый ЗаписьJSON; Ответ.УстановитьСтроку(); ЗаписатьJSON(Ответ,ОтветМассив); // сериализует ОтветМассив в формат JSON Возврат Ответ.Закрыть(); КонецФункции getusers" Функция ОсновнойGET(Запрос) Ответ = Новый HTTPСервисОтвет(200); Если НРЕГ(Запрос.ПараметрыЗапроса.Получить("action")) = "getusers" Тогда Ответ.УстановитьТелоИзСтроки(ВернутьСписокПользователей()); КонецЕсли; Возврат Ответ; КонецФункции Функция ВернутьСписокПользователей() Запрос = Новый Запрос; Запрос.Текст = "ВЫБРАТЬ | Пользователи.Ссылка |ИЗ | Справочник.Пользователи КАК Пользователи |ГДЕ | Пользователи.Недействителен = ЛОЖЬ"; Выборка = Запрос.Выполнить().Выбрать(); ОтветМассив = Новый Массив; Пока Выборка.Следующий() Цикл ОтветМассив.Добавить(Новый Структура("name,id",Выборка.Ссылка.Наименование,""+Выборка.Ссылка.УникальныйИдентификатор())); КонецЦикла; Ответ = Новый ЗаписьJSON; Ответ.УстановитьСтроку(); ЗаписатьJSON(Ответ,ОтветМассив); // сериализует ОтветМассив в формат JSON Возврат Ответ.Закрыть(); КонецФункции 

Check the result:

Result

HTTP-service debugging.

Configuring the connection debugger.

To debug HTTP-service to include (or make sure that inclusive) the following check boxes.

  1. Allow HTTP-services debugging publication menu 1C: Enterprise (Configurator / Administration / Publish to Web server ...).
    Enable debugging
    debugger Address - address of the computer where we will run the Configurator (note the syntax: «tcp: // <address debugger>").
  2. Enable automatic connection to the HTTP-server services (Configurator / Debugging / Connect ...).
    Set up autoconnection
    Set up autoconnection 2

Verify how it all works.

We set a breakpoint at the beginning of the function of the method GET:

Breakpoint

We update the page with the call of our service.

Site open

We are sure that our debugger successfully connected to the HTTP session-service:

Check breakpoint catch

Check value of variables

Conclusion.

In this article, we consider a simple example to quickly create HTTP-service for the purpose of development of mechanisms of work with him.

In conclusion I would like to mention about the possibility of creating a HTTP-services with URL parameters, for example:

Parts of link description

This feature is configured in the HTTP-service ShabloneURL.

For the above example, the template might look like this: «/ users / {idPolzovatelya} / *" or so "/ {SECTION} / {id} / *".

Another template example

In this method, the handler function template URL parameters can be accessed via Zapros.ParametryURL property like this:

Zapros.ParametryURL [ "idPolzovatelya"]

Example for template params

Note the sequence of processing HTTP-service templates.

In our example, the template used 2:

  1. / *
  2. / Users / {idPolzovatelya} / *

When calling http://10.211.55.3/base/hs/info/users/0b3dcecf-104e-11e6-9bdd-001c42ecfab6?action=disable work pattern 1, since URL parameters are also consistent with it and it is processed first. To avoid this, the first template is recommended to be changed to «/ i / *» for the unique URL matching.

Calling the first method, respectively, will also have a new URL:

Check new template 

1C:Enterprise Developer's Community