user2843864
user2843864

Reputation: 81

How do I write tickets to Zendesk API using POST JSON requests in VBA?

Note: I have posted this on Mr. Excel as well. Link here.

I am trying to write a ticket to a Zendesk API from Access/Excel using VBA. I keep receiving the following error:

"error":"Unprocessable Entity","message":"server could not parse JSON"

My code is as follows: (replacing my actual Zendesk domain, username, and token with "zendeskdomain", "username", and "token" respectively).

 Dim strURL As String, strParse() As String, jsonStr As String
 Dim hreq As New MSXML2.XMLHTTP60
 Dim tixScript As Object

 strURL = "https://zendeskdomain.zendesk.com/api/v2/tickets.json"
 hreq.Open "POST", strURL, 0, "username/token", "token"

 hreq.setRequestHeader "User-Agent", "Chrome/78.0.3904.108"
 hreq.setRequestHeader "Content-Type", "application/json"
 hreq.setRequestHeader "Accept", "application/json"

 jsonStr = """{""ticket"": {""subject"": ""Testing post requests"", ""requester_id"":393329203772, ""comment"": { ""body"": ""This will work"" }}}"""

 hreq.Send jsonStr

 MsgBox hreq.responseText

I was able to take the same JSON string and create a ticket using the actual Zendesk Developer API. I was also able to make a GET connection to work using the same three request headers used in the POST code above.

I feel like I am missing something simple, but I cannot seem to make it work.

Any help would be greatly appreciated.

Thank you

Upvotes: 0

Views: 838

Answers (1)

barrowc
barrowc

Reputation: 10679

The outer pairs of double quotes are unnecessary and make jsonStr contain invalid JSON. Using Debug.Print in VBA shows that jsonStr ends up containing:

"{"ticket": {"subject": "Testing post requests", "requester_id":393329203772, "comment": { "body": "This will work" }}}"

Change the line that sets jsonStr to:

jsonStr = "{""ticket"": {""subject"": ""Testing post requests"", ""requester_id"":393329203772, ""comment"": { ""body"": ""This will work"" }}}"

Upvotes: 1

Related Questions