API Call Works in Postman, Fails in Code

contacts

#1

I’m attempting to add new offline contacts using only email addresses. My code in C# is:

        var req = (HttpWebRequest)WebRequest.Create(@"https://api.hubapi.com/contacts/v1/contact/?hapikey={CompanyAPIKey}");
        req.ContentType = "application/json";
        req.Method = "POST";

        HubspotCreationContact hc = new HubspotCreationContact();
        hc.AddProperty("email", "Ryan.Brothers@nsc.com");
        string convertedToPost = JsonConvert.SerializeObject(hc);
        Console.WriteLine(convertedToPost.PrettyPrintJson());

        using (var sw = new StreamWriter(req.GetRequestStream()))
        {
            sw.Write(convertedToPost);
            sw.Flush();
        }
        
        try
        {
            response = (HttpWebResponse)req.GetResponse();
        }
        catch (WebException ex)
        {
            Console.WriteLine("WebException");
            Console.WriteLine();
            var resp = new StreamReader(ex.Response.GetResponseStream());
            var error = resp.ReadToEnd();
            Console.WriteLine(error);
        }

The generated JSON, copy/pasted from the console, is:

{
    "properties": [
        {
            "property": "email",
            "value": "test@test.com"
        }
    ]
}

This works perfectly from Postman, but returns a 400 error in code. Additionally, the resulting Response Stream is completely empty. I can actually use this same method to add existing contacts to a list in the same code body. Is there something special we have to be doing when creating contacts? I should mention, I copied the sample URL for creating contacts. I just replaced the api key with our company’s. Any help would be greatly appreciated.