Use POST method to get data from website

The 1C:Enterprise developers forum

#1
People who like this:0Yes/0No
Just came
Rating: 1
Joined: Dec 19, 2022
Company:

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

 
#2
People who like this:0Yes/0No
Administrator
Rating: 23
Joined: Oct 3, 2019
Company:

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.

 
#3
People who like this:0Yes/0No
Just came
Rating: 1
Joined: Dec 19, 2022
Company:

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

 
#4
People who like this:0Yes/0No
Just came
Rating: 1
Joined: Dec 19, 2022
Company:

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.

Edited: Bảo Vũ Bá - Jan 14, 2023 09:02 PM
 
#5
People who like this:0Yes/0No
Just came
Rating: 1
Joined: Dec 19, 2022
Company:

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.

 
#6
People who like this:1Yes/0No
Administrator
Rating: 23
Joined: Oct 3, 2019
Company:

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!

 
#7
People who like this:0Yes/0No
Just came
Rating: 1
Joined: Dec 19, 2022
Company:

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?

 
#8
People who like this:0Yes/0No
Just came
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

 
#9
People who like this:1Yes/0No
Administrator
Rating: 23
Joined: Oct 3, 2019
Company:

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

 
Subscribe
Users browsing this topic (guests: 1, registered: 0, hidden: 0)
Be the first to know tips & tricks on business application development!

A confirmation e-mail has been sent to the e-mail address you provided .

Click the link in the e-mail to confirm and activate the subscription.