Solved

C# HTTPWebRequest issue with JSON and POST

Posted on 2014-02-18
2
12,483 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
To properly understand GitHub, let’s divide it into two words ‘Git’ and ‘Hub’. Git is basically a ‘Distribution Version Control’ (DVC) and ‘Source Code Management’ (SCM) system widely used by software programmers while Hub means the efficient centre…
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…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

16 Experts available now in Live!

Get 1:1 Help Now