21) Call Restful API(JSON Service WebAPI) using Post Method in D365 FO (X++).
Example is to perform an Integration with JSON REST services using basic authentication method of adding Authorization header using HTTP Post method.
In below example i have done integration with ClearTax (3rd Party App).Where i send required value to ClearTax.
class JsonResonse
{
public static void main(Args _args)
{
str destinationUrl = 'https://einvoicing.internal.cleartax.co/v1/govt/api/Invoice/';
str requestJson, responseJson;
System.Net.HttpWebRequest request;
System.Net.HttpWebResponse response = new System.Net.HttpWebResponse();
CLRObject clrObj;
System.Byte[] bytes;
System.Text.Encoding utf8;
System.IO.Stream requestStream, responseStream;
System.IO.StreamReader streamReader;
System.Exception ex;
System.Net.WebHeaderCollection httpHeader;
System.Net.ServicePoint servicePt;
str byteStr;
System.Byte[] byteArray;
System.IO.Stream stream;
System.IO.Stream dataStream;
requestJson = @'{
"Version":"1.01",
"TranDtls":{
"TaxSch":"GST",
"SupTyp":"B2B",
"RegRev":"Y"
},
"DocDtls":{
"Typ":"INV",
"No":"Unique Invoice No.",
"Dt":"Today or Yesterday Day"
},
"SellerDtls":{
"Gstin":"29AAFCD5862R000",
"LglNm":"TAN TEST NI",
"TrdNm":"TAN TEST NI",
"Addr1":"TEST2",
"Addr2":"TEST2",
"Loc":"GANDHINAGAR",
"Pin":"560002",
"State":"KARNATAKA",
"Ph":"9999999999",
"Em":"ABC@GMAIL.COM"
},
"BuyerDtls":{
"Gstin":"37BZNPM9430M1kl",
"LglNm":"TAN TEST NI",
"Pos":"37",
"Addr1":"TEST2",
"Addr2":"TEST2",
"Loc":"GANDHINAGAR",
"Pin":"560002",
"State":"KARNATAKA",
"Ph":"9999999999",
"Em":"ABC@GMAIL.COM"
},
"ItemList":[
{
"SlNo":"1",
"IsServc":"N",
"PrdDesc":"dfdfsdf",
"HsnCd":"1001",
"Qty":"10",
"Unit":"bag",
"UnitPrice":10.0,
"TotAmt":151.0,
"Discount":0.0,
"AssAmt":151.0,
"GstRt":10.0,
"SgstAmt":0.0,
"IgstAmt":10.0,
"CgstAmt":0.0,
"CesRt":15.0,
"CesAmt":123.89,
"CesNonAdvlAmt":0.0,
"StateCesRt":36.0,
"StateCesAmt":151.0,
"StateCesNonAdvlAmt":0.0,
"OthChrg":0.0,
"TotItemVal":100.0
}
],
"ValDtls":{
"AssVal":100.0,
"CgstVal":0.0,
"SgstVal":0.0,
"IgstVal":0.0,
"CesVal":15.0,
"StCesVal":36.0,
"RndOffAmt":0.0,
"TotInvVal":0.0
}
}';
try
{
new InteropPermission(InteropKind::ClrInterop).assert();
httpHeader = new System.Net.WebHeaderCollection();
request = System.Net.WebRequest::Create(destinationUrl);
utf8 = System.Text.Encoding::get_UTF8();
bytes = utf8.GetBytes(requestJson);
request.set_Method("POST");
httpHeader.Add("Schema-Type:govt_schema_v1.1");
httpHeader.Add("Pass Owner Id");
httpHeader.Add("GSTIN");
request.set_Headers(httpHeader);
request.ContentType = 'application/json';
request.set_ContentLength(bytes.get_Length());
requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.get_Length());
response = request.GetResponse();
responseStream = response.GetResponseStream();
streamReader = new System.IO.StreamReader(responseStream);
responseJson = streamReader.ReadToEnd();
info(responseJson);
}
catch (Exception::CLRError)
{
ex = CLRInterop::getLastException().GetBaseException();
error(ex.get_Message());
}
requestStream.Close();
streamReader.Close();
responseStream.Close();
response.Close();
}
}
Comments
Post a Comment