Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# HTTPWebRequest issue with JSON and POST

Posted on 2014-02-18
2
Medium Priority
?
14,234 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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. 
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

926 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