Friday, October 12, 2018

Sample C# code to perform an HTTP POST method to a RESTlet

To invoke an HTTP POST request using C#, use the following code snippet as a guide:


C# code snippet:


//create customer record
Customer rec = new Customer();
rec.companyname = "Globe Telecom";
rec.isperson = false;


//create serializer to convert object into JSON string
JavaScriptSerializer js = new JavaScriptSerializer();
string jsonString = js.Serialize(rec);


//create HTTP request
WebRequest request = WebRequest.Create("https://rest.netsuite.com/app/site/hosting/restlet.nl?script=20&deploy=1");
request.ContentType = "application/json";
request.Method = "POST";
request.Headers.Add("Authorization:NLAuth nlauth_account=1234567,nlauth_email=email@netsuite.com,nlauth_signature=password,nlauth_role=3");


//write json string on the request body
using (var streamWriter = new StreamWriter(request.GetRequestStream())){ streamWriter.Write(jsonString); }


//send request and get response
WebResponse response = request.GetResponse();




Restlet code:


function saveCustomer(data){
    if(data != null && data != ""){
        var rec = nlapiCreateRecord('customer');
        if(data.isperson == false){
            rec.setFieldValue('isperson','F');
        }
        else { 
            rec.setFieldValue('isperson','T');
        }
        rec.setFieldValue('companyname',data.companyname);
        var id = nlapiSubmitRecord(rec);
        return id;
    }
}


No comments:

Post a Comment