Hi Nguyễn Long!
First you need to make a HTTP service in your server system (see screenshot 1).
Then this service must be published on the web server (screenshot 2).
After the service is published, it can be accessed using the POST request. Here is an example of 1C code for accessing a previously published HTTP service:
Code |
---|
structureToServer = New Structure;
structureToServer.Insert("email", email);
structureToServer.Insert("fullname", fullname);
JSONWriter = New JSONWriter;
JSONWriter.SetString();
WriteJSON(JSONWriter,structureToServer);
stringForRequest = JSONWriter.Close();
stringConnectionString = "Path to your server";
HTTPConnection = New HTTPConnection(stringConnectionString);
HTTPRequest = New HTTPRequest("YourServer/hs/ClientAPI/V1/ClientAPI");
HTTPRequest.SetBodyFromString(stringForRequest);
HTTPAnswer = HTTPConnection.Post(HTTPRequest);
If HTTPAnswer.StatusCode = 200 Then
Result = HTTPAnswer.GetBodyAsString();
EndIf;
|
The structure is created in the code. This structure has "email" and "fullname" fields. Then this structure is converted to the JSON format and transmitted to the server.
Then we get the response from the server (HTTPAnswer) and also convert it to the String (Result).
And this is how the data processing on the server will look:
Code |
---|
Function ClientAPIPOST_V1(Request)
stringFromClient = Request.GetBodyAsString("UTF-8");
JSONReader = New JSONReader;
JSONReader.SetString(stringFromClient);
structureFromClient = ReadJSON(JSONReader);
JSONReader.Close();
If structureFromClient.Property("fullname")
And ValueIsFilled(structureFromClient.fullname) Then
// Here you can write your code for data processing,
// for example, create a new document or make an entry in the Catalog
Response = New HTTPServiceResponse(200);
stringResponse = "Fullname field isn't empty";
Response.SetBodyFromString(stringResponse);
Else
Response = New HTTPServiceResponse(204); // 204 No Content
stringResponse = "Fullname field is empty";
Response.SetBodyFromString(stringResponse);
EndIf;
Return Response;
EndFunction |