We use cookies to make HubSpot's community a better place. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. To learn more, and to see a full list of cookies we use, check out our Cookie Policy (baked goods not included).
May 5, 2020 11:37 AM
// 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.
May 5, 2020 10:55 PM
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?
May 6, 2020 9:31 AM
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);
May 7, 2020 11:45 PM
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