Function GetToken() Export
ReturnParameters = New Structure;
ReturnParameters.Insert("Result");
ReturnParameters.Insert("Error", False);
structureBody = New Structure;
structureBody.Insert("clientId", Constants.Alaiko_ClientId.Get());
structureBody.Insert("clientSecret", Constants.Alaiko_ClientSecret.Get());
string_api_PATH = Constants.Alaiko_PathToAPI.Get();
string_api_Key = Constants.Alaiko_API_Key.Get();
HTTPConnection = New HTTPConnection(string_api_PATH,443,,,,, New OpenSSLSecureConnection);
ResourceAddress = "/user/login";
HTTPRequest = New HTTPRequest();
HTTPRequest.ResourceAddress = ResourceAddress;
HTTPRequest.Headers.Insert("Content-Type", "application/json");
HTTPRequest.Headers.Insert("x-api-key", string_api_Key);
JSONWriter = New JSONWriter;
JSONWriter.SetString();
WriteJSON(JSONWriter, structureBody);
resultJSON = JSONWriter.Close();
HTTPRequest.SetBodyFromString(resultJSON);
HTTPResponse = HTTPConnection.Post(HTTPRequest);
If HTTPResponse.StatusCode = 200 Then
JSONReader = New JSONReader;
JSONReader.SetString(HTTPResponse.GetBodyAsString());
ReturnParameters.Result = ReadJSON(JSONReader, False);
Else
ReturnParameters.Result = HTTPResponse.GetBodyAsString();
ReturnParameters.Error = True;
EndIf;
// ReturnParameters.ReturnParameters.Result.AccessToken
Return ReturnParameters;
EndFunction
Here a POST request is made to some resource. At the same time, we pass request parameters both in the headers of the request itself and in the request body.
I want to explain a little about my task.I have virtual server to request/report. I created http post services in 1C purpose that web page pushes the data to server for me. And now how do I read the data from that site? That website data is returned as JSON.
Hi,@Aleksandr Biryukov. When a website calls the API to push data to our server. How does it pass the authentication step? For example, we will use the password to enter the application.
Hi Aleksandr Biryukov, I found some documentation about JWT authentication and I want to try using it. Could you give me a simple example of JWT authentication?
Rating:
1
Joined: Jan 17, 2023
Company: Techatom Pvt Ltd
Hello this is Gulshan Negi In order to retrieve data from a website using the POST method, you'll need to send a request to the website's API endpoint. The API endpoint is the specific location on the website that provides access to its data. Hope it will help you. Thanks
with JWT authentication, a 1C program receives an access token from an external authentication server, and then uses this token to access the main server.
Here's what the source code might look like when connected to the main server:
Code
httpRequest = New HTTPRequest("http://example.com/hs/testService?AccessToken=" + accessToken);
OR
httpRequest = New HTTPRequest("http://example.com/hs/testService");
httpRequest.AddAccessToken(accessToken);
OR
headers = New Map;
headers.Insert("Authorization", "Bearer " + accessToken);
httpRequest = New HTTPRequest("http://example.com/hs/testService", headers);