Here is an example on how to create a HTTP service that returns data from the Items catalog. You need to do following:
Create a HTTP service, remember to set the Root URL, for example same as the name of the service.
Add an URL template, where specify template for parameters, for example: /item/{ItemCode}/ that means that instead of {ItemCode} one should pass a value of ItemCode parameter.
Add a method with the get name and a handler for it.
Publish HTTP services.
Use an URL of following structure to get data: http://<Infobase publication path>/hs/<HTTP service root URL><HTTP service template>, for example:
Code
http://localhost/Tests1cdn/hs/JSON/item/0000001/
Here is an example of the get method that finds a catalog item by its code and returns JSON-serialized value:
Code
Function ItemGet(Request)
Result = "";
ResultSuccessful = False;
If Request.URLParameters.Get("ItemCode") <> Undefined Then
ItemCode = Request.URLParameters.Get("ItemCode");
JSONWriter = New JSONWriter;
JSONWriterSettings = New JSONWriterSettings(, Chars.Tab);
JSONWriter.SetString(JSONWriterSettings);
Ref = Catalogs.Items.FindByCode(ItemCode);
If ValueIsFilled(Ref) Then
Item = Ref.GetObject();
XDTOSerializer.WriteJSON(JSONWriter, Item, XMLTypeAssignment.Explicit);
Result = JSONWriter.Close();
ResultSuccessful = True;
Else
Result = StrTemplate(NStr("en = 'The Items catalog does not contain an item with %1 code.'"), ItemCode);
EndIf;
Else
Result = NStr("en = 'Please, specify the item code.'");
EndIf;
Response = New HTTPServiceResponse(?(ResultSuccessful, 200, 404));
Response.SetBodyFromString(Result);
Return Response;
EndFunction
You can also serialize a structure and receive more common JSON structure:
Code
Function ItemGet(Request)
Result = "";
ResultSuccessful = False;
If Request.URLParameters.Get("ItemCode") <> Undefined Then
ItemCode = Request.URLParameters.Get("ItemCode");
JSONWriter = New JSONWriter;
JSONWriterSettings = New JSONWriterSettings(, Chars.Tab);
JSONWriter.SetString(JSONWriterSettings);
Ref = Catalogs.Items.FindByCode(ItemCode);
If ValueIsFilled(Ref) Then
Item = Ref.GetObject();
ItemStructure = New Structure;
ItemStructure.Insert("Code", Item.Code);
ItemStructure.Insert("Description", Item.Description);
WriteJSON(JSONWriter, ItemStructure);
Result = JSONWriter.Close();
ResultSuccessful = True;
Else
Result = StrTemplate(NStr("en = 'The Items catalog does not contain an item with %1 code.'"), ItemCode);
EndIf;
Else
Result = NStr("en = 'Please, specify the item code.'");
EndIf;
Response = New HTTPServiceResponse(?(ResultSuccessful, 200, 404));
Response.SetBodyFromString(Result);
Return Response;
EndFunction
I want to use HTTP -Services to receive data by Post method from a website or from a DataProcessor. When HTTP -Services receives the data after the post method in JSON format, it will save the data into the database and return the saved data to it. When you press the button to save the entire dose to HTTP -Services by POST method, in HTTP -Services will process the data and save it into PostsSON Catalogs, then return the result, I want you to help me perfect that small idea.