1c - 关于带有 Auth 的 Json Post

标签 1c

我有一个问题。 我想发布 json.它包含用户信息。我想读取返回的Json数据。 我写了示例代码。我的错误在哪里?我该怎么做?

示例网址:“api.abc.com/getlist/”

json输入={

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

我的代码:

    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;

最佳答案

尝试以下代码:

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


JSONWriter = New JSONWriter;
JSONWriter.SetString();
WriteJSON(JSONWriter, structureInput);

jsoninput = JSONWriter.Close();

然后:

HTTPRequest.Headers.Insert("Authorization", jsoninput);

关于1c - 关于带有 Auth 的 Json Post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72541046/

相关文章:

save - 尝试仅保存文档的一页

1c - 如何禁用 1C 服务器中的所有计划作业?

rest - 1C中如何通过OData获取额外的props?