1C HTTP services: create, publish, test and debug with examples
Checkcraft is a desktop app for checking APIs and logs without code, with a free edition. Download, no sign-up →
An HTTP service turns a 1C database into a REST API for a website, a mobile app or another system. Below is the whole path: the configuration object, URL template and handler, a JSON response, publishing, the /hs/ address, testing from outside and the usual integration errors.
How an HTTP service is built
In the configuration under Common you add an HTTP service with a root URL such as api. Inside are URL templates like /orders/{id}, each template has GET, POST and other methods, and each method has a handler in the service module. The handler gets an HTTPServiceRequest and returns an HTTPServiceResponse.
Where to find the service after publishing
After publishing the database on a web server the service is available at http://server/publication_name/hs/root_url/template. If the address returns 404, check that HTTP services are published by default or the service is ticked in the list.

How to test the service from outside
Send a request with curl or an API client using a 1C account and look at the status, headers and body. Check not just the happy path: a missing id, broken JSON in the body, a request without auth. Each should return a clear code and a JSON error, not a web server page.
How to save the checks and repeat them
The service changes with the configuration, and what worked yesterday may answer differently after an update. Save requests to the service with response rules and run them after every update. That is integration regression testing.

A handler that returns JSON
// Шаблон URL: /orders/{id}, метод GET, обработчик ЗаказGET
Функция ЗаказGET(Запрос)
Ид = Запрос.ПараметрыURL["id"];
Заказ = НайтиЗаказ(Ид); // ваша функция
Если Заказ = Неопределено Тогда
Возврат ОтветJSON(404, Новый Структура("error", "order not found"));
КонецЕсли;
Возврат ОтветJSON(200, Заказ);
КонецФункции
Функция ОтветJSON(Код, Значение)
Запись = Новый ЗаписьJSON;
Запись.УстановитьСтроку();
ЗаписатьJSON(Запись, Значение);
Ответ = Новый HTTPСервисОтвет(Код);
Ответ.Заголовки.Вставить("Content-Type", "application/json; charset=utf-8");
Ответ.УстановитьТелоИзСтроки(Запись.Закрыть(), "UTF-8", ИспользованиеByteOrderMark.НеИспользовать);
Возврат Ответ;
КонецФункцииContent-Type with charset and a body without a BOM spare the client half of the parsing problems. Return errors as JSON with the right code too, not as text.
Testing the service with curl
# GET существующего и несуществующего заказа
curl -i -u "Обмен:пароль" http://erp.example.local/trade/hs/api/orders/5001
curl -i -u "Обмен:пароль" http://erp.example.local/trade/hs/api/orders/999999
# POST с JSON
curl -i -u "Обмен:пароль" -H "Content-Type: application/json" \
--data '{"sku":"A-1","qty":2}' http://erp.example.local/trade/hs/api/orders
# без авторизации: ожидаем 401, а не страницу веб-сервера
curl -i http://erp.example.local/trade/hs/api/orders/5001The address is the web server publication name, the /hs/ segment, the service root URL and the template. More request examples in curl POST with JSON.
What to check in an HTTP service
| Check | Expected |
|---|---|
| Existing object | 200 and JSON with the right fields |
| Missing id | 404 and a JSON error |
| Broken JSON in a POST body | 400 and an error description |
| No auth | 401, not a web server page |
| User without rights | 403 |
| Cyrillic in data | Not garbled, UTF-8 |
Common problems
- 404 on the right address. The service is not published or published under another publication.
- The response is an HTML page. The error happened before the handler: auth, publishing, licence.
- Cyrillic is garbled. The body is not UTF-8 or charset is missing.
- The client cannot parse JSON. A BOM at the start of the body, see unexpected character when reading JSON.
More on 1C
Section overview and all guides: 1C integration via API: options, architecture and examples.
- HTTP requests from 1C: GET, POST, headers, auth and timeouts with examples
- HTTP request failed in 1C: causes and fixes
- OData in 1C: setup, queries and REST integration with examples
- 1C web services (SOAP): create, publish, WSDL and calls with examples
- Publishing 1C on IIS and Apache: step by step and common errors
Frequently asked questions
What address does a 1C HTTP service have?
http://server/publication/hs/root_url/template after publishing the database with HTTP services.
How do I return JSON?
Write the value with JSONWriter, create an HTTPServiceResponse with the code, add Content-Type: application/json; charset=utf-8 and set the body without a BOM.
How do I test it without coding?
Send requests with curl or an API client using a 1C account and check code, headers and body, including error cases.
Repeat this on your own API
Checkcraft is a desktop app that saves API response checks without code and shows what did not match. Start with a sample, then use your own sanitized data. Free runs on Windows and Linux with no time limit.
Download FreeDownload and first runNo sign-up or email needed, the archive is ready in a couple of minutes. See the edition limits.
References
Start with the linked example and compare its result with your own test requirement.