
Blog · 📚 1С: практика разработчика
1C in practice: integration via HTTP services — REST API from configuration
🌐 Machine-translated from Russian. Show original
In this part:
When you need an HTTP service#
The HTTP service turns 1C into a REST API: the site receives balances and prices, the mobile application creates orders, and the external system sends documents. Unlike file export, data is current at the time of request.
Service creation#
- In the configuration or extension: General → HTTP services → Add.
- The root URL — for example,
api. The service address will behttps://сервер/база/hs/api/.... - Add a URL template — for example,
/products/{code}— and a GET method with a handler.
Handler: passing JSON#
Функция ProductsGET(Запрос)
Код = Запрос.ПараметрыURL["code"];
Товар = Справочники.Номенклатура.НайтиПоКоду(Код);
Если Товар.Пустая() Тогда
Возврат ОтветJSON(404, Новый Структура("error", "Товар не найден"));
КонецЕсли;
Данные = Новый Структура;
Данные.Вставить("code", Код);
Данные.Вставить("name", Строка(Товар));
Возврат ОтветJSON(200, Данные);
КонецФункции
Функция ОтветJSON(Код, Данные)
Запись = Новый ЗаписьJSON;
Запись.УстановитьСтроку();
ЗаписатьJSON(Запись, Данные);
Ответ = Новый HTTPСервисОтвет(Код);
Ответ.Заголовки.Вставить("Content-Type", "application/json; charset=utf-8");
Ответ.УстановитьТелоИзСтроки(Запись.Закрыть());
Возврат Ответ;
КонецФункции
We accept data: POST#
Функция OrdersPOST(Запрос)
Чтение = Новый ЧтениеJSON;
Чтение.УстановитьСтроку(Запрос.ПолучитьТелоКакСтроку());
Заказ = ПрочитатьJSON(Чтение);
Чтение.Закрыть();
// проверка полей, создание документа...
Возврат ОтветJSON(201, Новый Структура("id", "..."));
КонецФункции
Always check input data: required fields, types, length. Return an error with code 400 with a clear message.
Publication and verification#
HTTP services operate through a web server: publish a database with HTTP services enabled (see publication in IIS or Apache in the 1C server series). Verification:
curl -u api_user:пароль https://1c.example.kz/buh/hs/api/products/00-00000123
Authentication and rights#
- Create a separate 1C user to integrate with minimal permissions — only to the required objects.
- Use HTTPS: Basic authentication without encryption transmits the password in plain text.
- In the HTTP service properties, session reuse can be configured — this reduces overhead for each call.
Common mistakes#
- 404 on /hs/... — service not published or invalid root URL.
- 401 — incorrect user or does not have rights to the HTTP service.
- Crab gills — no encoding
charset=utf-8specified in Content-Type. - Slow responses — heavy requests or loop requests within the handler, see the performance section.
Comments · 0
No comments yet.Sign in to comment, rate and see all comments.