Solved

C# HTTPWebRequest issue with JSON and POST

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
performance tune IIS 10 on win 2016 that only runs one website 4 58
Printing 1 51
Hey!!!! 1 20
How come this regular expression allows "e"? 14 35
The task of choosing a web design company to build a website for your business should never be taken in a light manner. Provided the fact that your website will act as a representative to your business and will be responsible for imposing an online …
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

785 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