Solved

C# HTTPWebRequest issue with JSON and POST

Posted on 2014-02-18
2
12,306 Views
Last Modified: 2014-02-18
Hi I am using an api called HubSpot to update lead data on the HubSpot portal.
Here is the link to the Contact Update call -
http://developers.hubspot.com/docs/methods/contacts/update_contact
Here is the way you are supposed to POST -
https://api.hubapi.com/contacts/v1/contact/vid/61571/profile?hapikey=demo

and you pass JSON data like this -

{
  "properties": [
    {
      "property": "lifecyclestage",
      "value": "customer"
    }
  ]
}

I know the code works because if the JSON data is correctly formed everything updates fine and there are no errors
If the JSON data is not formed correctly I get an error 400  -
System.Net.WebException: The remote server returned an error: (400) Bad Request.
When this line is hit -
response = (HttpWebResponse)request.GetResponse();
the C# code immediately hits the "catch" block and my response variable is null -

However, if I If I process the same POST (with improperly formed JSON) using POSTMAN (a Google Chrome plugin), I get more information.  
I still get the error 400 Bad Request, but I actually see return JSON which gives more detail about the error.

{
    "status": "error",
    "message": "[{\"property\":\"Lifecyclestage\",\"msg\":\"Property \\\"Lifecyclestage\\\" does not exist\",\"error\":\"PROPERTY_DOESNT_EXIST\"}]",
    "requestId": "330ed575-902b-4d56-a9d9-2a63499cdaa3"
}

My question is how do I get this returned JSON data in my C# code since the response variable never gets set because it errors out.
0
Comment
Question by:kevinvw1
2 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 39868076
Make sure you're catching the right type of exception (WebException) and then access its Response property. Here's a simple Console application that shows how to do it.

using System;
using System.IO;
using System.Net;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main( string[] args )
        {
            try
            {
                WebRequest request = WebRequest.Create( "https://api.hubapi.com/contacts/v1/contact/vid/61571/profile?hapikey=demo" );
                var response = (HttpWebResponse)request.GetResponse();
            }
            catch( WebException ex )
            {
                StreamReader sr = new StreamReader( ex.Response.GetResponseStream(), true );
                Console.WriteLine( sr.ReadToEnd() );
            }
            finally
            {
                Console.ReadKey();
            }
        }
    }
}

Open in new window

0
 

Author Comment

by:kevinvw1
ID: 39868410
Yes!  That was exactly the issue.  I know just enough about C# to be dangerous.  I didn't realize there were different exception classes for catch.

Thanks for teaching me something very valuable.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

New Relic recently released its Synthetics product that allows for the creation of performance monitors that periodically test a site's performance. If you wish to test an interactive workflow New Relic employs Selenium WebDriverJS to run those test…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now