Desktop version

Main > Forum > 1C:Enterprise Platform > 1C:Enterprise – Business applications platform > About Json Post with Auth.

Forum

Search UsersRules
About Json Post with Auth.
#1
Active user
Points:: 0
Joined:: May 9, 2018

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;

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

Hello Erdoğan,

and what is your question?

Profile
#3
Active user
Points:: 0
Joined:: May 9, 2018

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"
         
        }'

Profile
#4
Just came
Points:: 0
Joined:: Oct 5, 2018

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 ?

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

Hello Erdoğan,

I saw your question on stackoverflow.

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

Profile
#6
Active user
Points:: 0
Joined:: May 9, 2018

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

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

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

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

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.

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.