I am working on a FreshDesk integration where once the user clicks “Send”, I attempt to create a FreshDesk ticket via their API (https://developers.freshdesk.com/api/#create_ticket). I can create a ticket just fine using
[code] Dim response, reqMethod As String
Dim HTTPSFreshDesk As new HTTPSecureSocket
Dim requestURL As String = “https://{domain}.freshdesk.com/api/v2/” + request
HTTPSFreshDesk.Secure = True
HTTPSFreshDesk.SetRequestContent(value, “application/json”)
HTTPSFreshDesk.SetRequestHeader(“Authorization”, “Basic [token]”)
HTTPSFreshDesk.RequestHeaders.AppendHeader(“Content-Type”, “application/json”)
HTTPSFreshDesk.Yield = True
HTTPSFreshDesk.ConnectionType = SSLSocket.SSLv23
Select Case httpMethod
Case enumHTTPMethods.POST
reqMethod = “POST”
Case enumHTTPMethods.DELETE
reqMethod = “DELETE”
Case else
Return “{}”
End Select
response = HTTPSFreshDesk.SendRequest(reqMethod, requestURL, 5)
Return response
[/code]
The problem comes when I include an attachment using
[code] Dim response, reqMethod As String
Dim HTTPSFreshDesk As new HTTPSecureSocket
Dim requestURL As String = “https://{domain}.freshdesk.com/api/v2/” + request
Dim Boundary As String
Static CRLF As String = EndOfLine.Windows
Dim data As New MemoryBlock(0)
Dim out As New BinaryStream(data)
Boundary = “–” + Right(EncodeHex(MD5(Str(Microseconds))), 24) + “-bOuNdArY”
For Each key As String In d.Keys
out.Write(“–” + Boundary + CRLF)
If VarType(d.Value(Key)) = Variant.TypeString Then
out.Write(“Content-Disposition: form-data; name=”“” + key + “”“” + CRLF + CRLF)
out.Write(d.Value(key) + CRLF)
ElseIf d.Value(Key) IsA FolderItem Then
Dim file As FolderItem = d.Value(key)
out.Write(“Content-Disposition: form-data; name=”“” + key + “”“; filename=”“” + File.Name + “”“” + CRLF)
out.Write(“Content-Type: application/json” + CRLF + CRLF) ’ replace with actual MIME Type
Dim bs As BinaryStream = BinaryStream.Open(File)
out.Write(bs.Read(bs.Length) + CRLF)
bs.Close
End If
Next
out.Write(“–” + Boundary + “–” + CRLF)
out.Close
HTTPSFreshDesk.Secure = True
HTTPSFreshDesk.Yield = True
HTTPSFreshDesk.ConnectionType = SSLSocket.SSLv23
HTTPSFreshDesk.SetRequestHeader(“Authorization”, “Basic [token]”)
HTTPSFreshDesk.RequestHeaders.AppendHeader(“Content-Type”, “multipart/form-data”)
HTTPSFreshDesk.SetRequestContent(data, “multipart/form-data; boundary=” + Boundary)
'HTTPSFreshDesk.SetRequestContent(value, “application/json”)
'HTTPSFreshDesk.RequestHeaders.AppendHeader(“Content-Type”, “application/json”)
Select Case httpMethod
Case enumHTTPMethods.POST
reqMethod = “POST”
Case enumHTTPMethods.DELETE
reqMethod = “DELETE”
Case else
Return “{}”
End Select
response = HTTPSFreshDesk.SendRequest(reqMethod, requestURL, 5)
Return response[/code]
Yet cURL works perfectly…
curl -v -u {username}:{password} -F "attachments[]=@/home/administrator/up.csv" -F "email=me@example.com" -F "subject=Ticket Title" -F "description=this is a sample ticket" -F "status=2" -F "priority=1" -X POST 'https://{domain}.freshdesk.com/api/v2/tickets'
Thoughts?