Posting out to 3rd party API
Example of taking data in the form of a Model from your site and sending it out somewhere else and getting back a httpresponsemessage.
[HttpPost]
public HttpResponseMessage Post(Appt_Model_DTO appt)
{
>>>>You could eliminate this portion
if (ModelState.IsValid)
{
//not necessary but you can run some checks to make sure the data is good
//that the model is valid and there is not
//you posted this data from your MVC site to your API Controller
//Now we are going to send it off to a third party site
if (appt.ID == null) {
appt.ID = my_provider.getRecordwithSameIdentity(appt.AccountCode);
}
appt.StartDateTime = DateTime.Now;
if (appt.POSID ==0)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
>>>>>> And start here
ZData z = new ZData();
z.AccountCode = appt.AccountCode;
z.AbbrevConceptName = appt.ConceptCode;
z.Comments = appt.Cust_Comments;
//Below line will need this reference declared using Newtonsoft.Json;
string data = JsonConvert.SerializeObject(z);
// Prepare web request...
//Declare the URL you will be posting to
string myurl = "https://apiaddress.com/api/WebCall?v=1.00&key=31f9f4ff-811e-";
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(myurl);
try {
myRequest.Method = "POST";
myRequest.KeepAlive = true;
myRequest.ContentType= "application/json";
myRequest.ContentLength = data.Length;
var streamWriter = new StreamWriter(myRequest.GetRequestStream());
streamWriter.Write(data);
streamWriter.Close();
//Declare Response Object
HttpWebResponse webresponse;
webresponse = (HttpWebResponse)myRequest.GetResponse();
//you could record response in a table
return new HttpResponseMessage(webresponse.StatusCode);
}
catch (Exception e)
{
//write exception somewhere or send error email and return a failed response
SendErrorEmail s = new SendErrorEmail();
s.Send_Error(e);
return new HttpResponseMessage(HttpStatusCode.ExpectationFailed);
}
[HttpPost]
public HttpResponseMessage Post(Appt_Model_DTO appt)
{
>>>>You could eliminate this portion
if (ModelState.IsValid)
{
//not necessary but you can run some checks to make sure the data is good
//that the model is valid and there is not
//you posted this data from your MVC site to your API Controller
//Now we are going to send it off to a third party site
if (appt.ID == null) {
appt.ID = my_provider.getRecordwithSameIdentity(appt.AccountCode);
}
appt.StartDateTime = DateTime.Now;
if (appt.POSID ==0)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
>>>>>> And start here
ZData z = new ZData();
z.AccountCode = appt.AccountCode;
z.AbbrevConceptName = appt.ConceptCode;
z.Comments = appt.Cust_Comments;
//Below line will need this reference declared using Newtonsoft.Json;
string data = JsonConvert.SerializeObject(z);
// Prepare web request...
//Declare the URL you will be posting to
string myurl = "https://apiaddress.com/api/WebCall?v=1.00&key=31f9f4ff-811e-";
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(myurl);
try {
myRequest.Method = "POST";
myRequest.KeepAlive = true;
myRequest.ContentType= "application/json";
myRequest.ContentLength = data.Length;
var streamWriter = new StreamWriter(myRequest.GetRequestStream());
streamWriter.Write(data);
streamWriter.Close();
//Declare Response Object
HttpWebResponse webresponse;
webresponse = (HttpWebResponse)myRequest.GetResponse();
//you could record response in a table
return new HttpResponseMessage(webresponse.StatusCode);
}
catch (Exception e)
{
//write exception somewhere or send error email and return a failed response
SendErrorEmail s = new SendErrorEmail();
s.Send_Error(e);
return new HttpResponseMessage(HttpStatusCode.ExpectationFailed);
}
Comments