APIs & Integrations

odowdbrendan
参加者

Updating a Contact by Email Address C#

// Get contact Id by email and update it
string api = "API_KEY"; 
string emailAddress = args[1].ToLower();

IRestResponse response = null;
IRestClient client = new RestClient("https://api.hubapi.com/");
IRestRequest request = new RestRequest("contacts/v1/contact/email/{email}/profile/", Method.PUT);
request.AddUrlSegment("email", emailAddress);
request.AddQueryParameter("hapikey", api);

var body = new
{
properties = new[]
{
new {property = "firstname", value = "Brendan" }
}
};

request.AddJsonBody(body);
response = client.Execute(request);

My code is above.  I cannot get the POST /contacts/v1/contact/email/:email/profile endpoint to work for me.  I am using C#.  Any advice is greatly appreciated.

0 いいね!
3件の返信
WendyGoh
HubSpot Employee
HubSpot Employee

Updating a Contact by Email Address C#

Hey @odowdbrendan,

 

Based on your code, it looks like you're using the method - PUT.

 

However the HTTP method for this endpoint - Update an existing contact by email | Contacts API is - POST.

 

Could you try changing that out and see if it works?

0 いいね!
odowdbrendan
参加者

Updating a Contact by Email Address C#

Updated to use POST, still did not work. I think it might be how the var body is formatted, but I am not sure how to change it?

   string api = "api_key"; /
                string emailAddress = args[1].ToLower();

                IRestResponse response = null;
                IRestClient client = new RestClient("https://api.hubapi.com/");
                IRestRequest request = new RestRequest("contacts/v1/contact/email/{email}/profile/", Method.POST);
                request.AddUrlSegment("email", emailAddress);
                request.AddQueryParameter("hapikey", api);

                var body = new
                {
                    properties = new[]
                    {
                        new {property = "firstname", value = "Brendan" }
                    }
                 };

                request.AddJsonBody(body);
                response = client.Post(request);
0 いいね!
WendyGoh
HubSpot Employee
HubSpot Employee

Updating a Contact by Email Address C#

Hey @odowdbrendan,

 

Two things here:

 

1. I noticed that you're missing the content type: application/json. Could you try adding that and see if it works?

 

2. If it doesn't, you might want to try changing your code to use something like this:

 

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    string json = new JavaScriptSerializer().Serialize(new
                {
                    user = "Foo",
                    password = "Baz"
                });

    streamWriter.Write(json);
}

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();
}

 

As referencing this stackoverflow discussion: https://stackoverflow.com/questions/9145667/how-to-post-json-to-a-server-using-c

 

 

0 いいね!