Mutton92
Mutton92

Reputation: 91

Use my own form before submitting data to Marketo

I am using ASP.NET and would like to use my own form, but still send the data into a Marketo form to create a new lead. I have got the field names and values that I would like to submit formatted in JSON as shown in the Marketo documentation for create/update a lead - http://developers.marketo.com/documentation/rest/createupdate-leads/

{  
   "action":"createOnly",
   "input":[  
      {  
         "email":"[email protected]",
         "firstName":"Kataldar-1",
         "postalCode":"04828"
      }
   ]
}

The form will only ever create one lead per form, I have created the form in Marketo and have the form id and everything that I think I need, I just don't know what I need to do in order to submit the values into the Marketo form.

Ideally I would like to use the SOAP or REST api to achieve my goal from the server side rather than using javascript.

I'm not against the server-side form post method and if anyone could help with the server side code needed with regards to that it would also be appreiciated

Upvotes: 1

Views: 1035

Answers (1)

Mutton92
Mutton92

Reputation: 91

With the help of Kenny Elkington from the Marketo dev team I was able to solve the problem!

The first thing that I found out was that to use the REST method I first needed to follow the instructions here -

http://developers.marketo.com/blog/quick-start-guide-for-marketo-rest-api/

In order to be able to generate an authentication token.

Then I could use -

private String getToken()
{
    String url = host + "/identity/oauth/token?grant_type=client_credentials&client_id=" + clientId + "&client_secret=" + clientSecret;
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.ContentType = "application/json";
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream resStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(resStream);
    String json = reader.ReadToEnd();
    Dictionary<String, String> dict = JsonConvert.DeserializeObject<Dictionary<String, String>>(json);
    return dict["access_token"];
}

Then once I had the access token I was able to authenticate myself which allowed me to create a lead as follows -

private String host = "" //host of your marketo instance, https://AAA-BBB-CCC.mktorest.com
String url = host + "/rest/v1/leads.json?access_token=" + getToken();

String requestBody = "----JSON INPUT----";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json";
request.Accept = "application/json";
StreamWriter wr = new StreamWriter(request.GetRequestStream());
wr.Write(requestBody);
wr.Flush();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader reader = new StreamReader(resStream);
String responseString = reader.ReadToEnd();

I could then use the response string in order to determine if the lead was successfully create or I could output the error message to the user.

Upvotes: 1

Related Questions