NOTE: This page has been translated automatically from Russian to English. Original page.
The HOWTO: Create and debug HTTP-service in the 1C: Enterprise
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».
Next, add to it a URL pattern for simplicity, let's call him "master". The value of the template should be equal to "/ *".
By the pattern we add the GET method: name = «GET», HTTP-method = «GET».
Create a handler for GET HTTP-method.
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:
HTTP-service debugging.
Configuring the connection debugger.
To debug HTTP-service to include (or make sure that inclusive) the following check boxes.
- Allow HTTP-services debugging publication menu 1C: Enterprise (Configurator / Administration / Publish to Web server ...).
debugger Address - address of the computer where we will run the Configurator (note the syntax: «tcp: // <address debugger>"). - Enable automatic connection to the HTTP-server services (Configurator / Debugging / Connect ...).
Verify how it all works.
We set a breakpoint at the beginning of the function of the method GET:
We update the page with the call of our service.
We are sure that our debugger successfully connected to the HTTP session-service:
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:
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} / *".
In this method, the handler function template URL parameters can be accessed via Zapros.ParametryURL property like this:
Zapros.ParametryURL [ "idPolzovatelya"]
Note the sequence of processing HTTP-service templates.
In our example, the template used 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: