Call third party API from D365F&O through code x++, with Authentication key and user login

 Hi viewers,


Please check the code to call third-party API from D365 F&O through code x++.

For authentication sample code:

  public str getKey()
  {
      System.Net.HttpWebRequest request;
      System.IO.Stream stream;
      System.Exception sysEx;
      str responseBody;
      container con;
      request = System.Net.WebRequest::Create(" URL") as System.Net.HttpWebRequest;
      request.Method = 'post';
      request.ContentType = 'application/json';
      str jsonstring = strFmt('%1%2%3%4%5%6%7','{','"login"',':','"info@****.com",','"password":','"******"','}');
      var utf8 = System.Text.Encoding::get_UTF8();
      var byteArrayPayload = utf8.GetBytes(jsonstring);
      using (System.IO.Stream dataStream = request.GetRequestStream())
      {
          dataStream.Write(byteArrayPayload, 0, byteArrayPayload.Length);
      }
      using (System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse)
      {
          stream = response.GetResponseStream();
          System.IO.StreamReader reader = new System.IO.StreamReader(stream);
          responseBody = reader.ReadToEnd();
          str strreplace =  strReplace(responseBody, ':',',');
          strreplace = strRem(strreplace, '"');
          strreplace = strRem(strreplace, '\\');
          con =  str2con(strreplace, ',');
      }
      return conPeek(con,2);
  }

 To send request and get response from API. :


       System.Net.HttpWebRequest request;
        System.IO.Stream stream;
        System.Exception sysEx;
        str responseBody;
        container con;
        str keyValue;
        str requestID;
        str jsonstring;
        if(_question != '')
        {
            keyValue = this.getKey();
             requestID  = this.updateRequest(_question);
            request = System.Net.WebRequest::Create("url") as System.Net.HttpWebRequest;
            request.Method = 'post';
            request.ContentType = 'application/json';
            jsonstring = strFmt('%1%2%3%4%5%6%7%8%9%10%11%12%13%14','{','"Parameter":','"' ,ParameterValue,'",','"carrier_endpoint":','"URL",','"node_endpoint":','"URL",','"X-Token":','"',keyValue,'"', '}');
           var utf88 = System.Text.Encoding::get_UTF8();
            var byteArrayPayloads = utf88.GetBytes(jsonstring);
            using (System.IO.Stream dataStream = request.GetRequestStream())
            {
                dataStream.Write(byteArrayPayloads, 0, byteArrayPayloads.Length);
            }
            using (System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse)
            {
                stream = response.GetResponseStream();
                System.IO.StreamReader reader = new System.IO.StreamReader(stream);
                responseBody = reader.ReadToEnd();
            }
        }
        else
        {
            throw error('No question');
        }

  

    


Comments

Popular posts from this blog

Cannot select a record for update when the transaction is not started on the user connection attached. You need to begin transaction on the user connection first. in d365F&O

Multi select lookup in UI Builder class in D365F&O through x++ code.

Records to include in SSRS Report With UI Builder class