kevinvw1
asked on
C# HTTPWebRequest issue with JSON and POST
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.G etResponse ();
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\":\"Lifecycl estage\",\ "msg\":\"P roperty \\\"Lifecyclestage\\\" does not exist\",\"error\":\"PROPER TY_DOESNT_ EXIST\"}]" ,
"requestId": "330ed575-902b-4d56-a9d9-2 a63499cdaa 3"
}
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.
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.G
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\":\"Lifecycl
"requestId": "330ed575-902b-4d56-a9d9-2
}
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for teaching me something very valuable.