About Json Post with Auth.

The 1C:Enterprise developers forum

#1
People who like this:0Yes/0No
Active user
Rating: 2
Joined: May 9, 2018
Company: GOLDENNET BİLGİ TEKNOLOJİLERİ

Hello. I have a question.
I want to post json. It contains user information. I want to read the returned Json data.

example url : "api.abc.com/getlist/"

jsoninput = {  
         
              "MERCHANT":"merchid",
              "MERCHANT_KEY":"merchkey",
              "START_DATE":"2022-01-01",
              "END_DATE":"2022-02-02"
           
        }


My Code :

       ServerAdress = "api.abc.com";
Resource = "getlist";
ssl1 = new OpenSSLSecureConnection(Undefined, Undefined);
HTTP = New HTTPConnection(ServerAdress,443,,,,10,ssl1);


HTTPRequest = New HTTPRequest("getlist");
HTTPRequest.Headers.Insert("Authorization", jsoninput);
HTTPRequest.Headers.Insert("Accept",  "application/json");
       HTTPRequest.Headers.Insert("Content-Type","application/x-www-form-urlencoded");
       HTTPRequest.Headers.Insert("Accept-Language",  "en_US");
       HTTPRequest.Headers.Insert("Cache-Control",    "no-cache");
       HTTPRequest.SetBodyFromString("grant_type=client_credentials");
Connection = New HTTPConnection("api.abc.com",,,,,10,New OpenSSLSecureConnection);

       JSONReader = New JSONReader;
 
Try        
           HTTPAnswer = Connection.Post(HTTPRequest); // post Request
           JSONReader.SetString(HTTPAnswer.GetBodyAsString());
       Data = ReadJSON(JSONReader, False);
           message(Data);
       Except
   ErrorText = "Sending " + ErrorDescription();
   message(ErrorText);
EndTry;

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

Hello Erdoğan,

and what is your question?

 
#3
People who like this:0Yes/0No
Active user
Rating: 2
Joined: May 9, 2018
Company: GOLDENNET BİLGİ TEKNOLOJİLERİ

I am very sorry. I forgot to write my question. There is an Api service. I want to post a value here. The json information contains login information. Webservice url: "api.abc.com/getlist/". I want to send this as a parameter. I want to read the returned value as json.

jsonparameters= '{
       
              "MERCHANT":"merchid",
              "MERCHANT_KEY":"merchkey",
              "START_DATE":"2022-01-01",
              "END_DATE":"2022-02-02"
         
        }'

 
#4
People who like this:0Yes/0No
Just came
Rating: 0
Joined: Oct 5, 2018
Company:

Hi Erdoğan Uğuz .
If I understand you correctly you need to put parameters into the request body. But you have another value there.
HTTPRequest.SetBodyFromString("grant_type=client_credentials");
You can do something like this
HTTPRequest.SetBodyFromString(jsonparameters);
Then maybe to add parameter grant_type to your json ?

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

Hello Erdoğan,

I saw your question on stackoverflow.

Please email me the documentation for this service, and I'll try to help you

 
#6
People who like this:0Yes/0No
Active user
Rating: 2
Joined: May 9, 2018
Company: GOLDENNET BİLGİ TEKNOLOJİLERİ

Thank you.
I solved the problem.

I fixed the code. Worked.

Code
jsoninput = "{  
         
              ""MERCHANT"":""merchid"",
              ""MERCHANT_KEY"":""merchkey"",
              ""START_DATE"":""2022-01-01"",
              ""END_DATE"":""2022-02-02""
           
        }";

HTTPRequest.SetBodyFromString(jsoninput);

Edited: Erdoğan Uğuz - Dec 12, 2022 05:34 AM
 
#7
People who like this:1Yes/0No
Administrator
Rating: 33
Joined: Oct 3, 2019
Company:

Hello Erdoğan,

as I understand it, you had a problem with the creation of JSON. You create it manually now. But try the following code:

Code
Procedure createParameters()

   merchid = "merchid";
   merchkey = "merchkey";
      
   structureParameters = New Structure;
   
   structureParameters.Insert("MERCHANT",       merchid);
   structureParameters.Insert("MERCHANT_KEY",    merchkey);
   structureParameters.Insert("START_DATE",    "2022-01-01");
   structureParameters.Insert("END_DATE",       "2022-02-02");
   
   resultJSON = toJSON(structureParameters);

EndProcedure

Function toJSON(Value)
   
   JSONWriter = New JSONWriter;
   JSONWriter.SetString();
   
   WriteJSON(JSONWriter,Value);
   
   Return JSONWriter.Close();
   
EndFunction

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

1C has good tools for working with JSON and it is more convenient to use them than to create JSON manually every time.

And the probability of error in this case is much lower.

 
Subscribe
Users browsing this topic (guests: 2, 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.