Desktop version

Main > Forum > 1C:Enterprise Platform > 1C:Enterprise – Business applications platform > Use POST method to get data from website

Forum

Search UsersRules
Use POST method to get data from website
#1
Just came
Points:: 0
Joined:: Dec 19, 2022

Hello! Can anyone help me with creating a POST request?

Profile
#2
Administrator
Points:: 0
Joined:: Oct 3, 2019

Hi Bảo Vũ Bá,

please see the following example:

Code
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.

Look. If there are any questions, I will answer.

Profile
#3
Just came
Points:: 0
Joined:: Dec 19, 2022

Could you please explain this code to me?
 
  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();

And could you explain a bit more about how this whole code works? thank you so much

Profile
#4
Just came
Points:: 0
Joined:: Dec 19, 2022

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.

Profile
#5
Just came
Points:: 0
Joined:: Dec 19, 2022

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.

Profile
#6
Administrator
Points:: 0
Joined:: Oct 3, 2019

Hi Bảo Vũ Bá,

yes, I understand your request and will try to help you.

But first, would you mind reading these articles:

https://1c-dn.com/blog/work-with-http-services-in-1c-part-1-get-method/
https://1c-dn.com/blog/work-with-http-services-in-1c-part-2-post-method/
https://1c-dn.com/blog/how-to-integrate-1c-and-trello/

These articles describe how to work with HTTP requests.

Please take a look at them, it seems to me that after that it will become more clear to you how to make your app.

And of course, I'm always ready to answer your questions!

Profile
#7
Just came
Points:: 0
Joined:: Dec 19, 2022

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?

Profile
#8
Just came
Points:: 0
Joined:: Jan 17, 2023

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

Profile
#9
Administrator
Points:: 0
Joined:: Oct 3, 2019

Hello Bảo Vũ Bá,

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);   

Profile
Subscribe
Users browsing this topic (guests: 1, registered: 0, hidden: 0)



© 1C LLC. All rights reserved
1C Company respects the privacy of our customers and visitors
to our Web-site.