Parsing WebRequest.Entity (HTTP Post data)

I’m working on a Web Service using the “HandleURL” event. When data is posted to my service, the WebRequest.Entity contains the following:

----0D5F3D31A53D5194D2542A8A-bOuNdArY
Content-Disposition: form-data; name="CompanyID"

1
----0D5F3D31A53D5194D2542A8A-bOuNdArY
Content-Disposition: form-data; name="UnixTime"

1539337727
----0D5F3D31A53D5194D2542A8A-bOuNdArY
Content-Disposition: form-data; name="Timezone"

America/New_York
----0D5F3D31A53D5194D2542A8A-bOuNdArY
Content-Disposition: form-data; name="DepartmentID"

Plant-OPS
----0D5F3D31A53D5194D2542A8A-bOuNdArY
Content-Disposition: form-data; name="UUID"

F4C30223-CB0F-428A-A241-AB3A47E4D9A0
----0D5F3D31A53D5194D2542A8A-bOuNdArY
Content-Disposition: form-data; name="FirstName"

Lawrence
----0D5F3D31A53D5194D2542A8A-bOuNdArY
Content-Disposition: form-data; name="LastName"

Westhaver

Are there any built-in methods for parsing the POST’ed data or do I have to roll-my-own?

-Wes

You’ll have to make your own. The good news is that the boundary string will be in the headers, so all you’ll need to do is Split on that string to get an array of items, loop over those items and Split each on EndOfLine.Windows to split the header and data portions. RegEx could easily be used to extract the parameter name.

Extracted from a project of mine:

[code]Function MultipartParse(FormData As String, Boundary As String) As Dictionary
Static CRLF As String = EndOfLine.Windows
Dim form As New Dictionary
Dim elements() As String = Split(FormData, “–” + Boundary)’ + CRLF)

Dim ecount As Integer = UBound(elements)
For i As Integer = 1 To ecount
Dim line As String = NthField(elements(i).LTrim, CRLF, 1)
Dim name As String = NthField(line, “;”, 2)
name = NthField(name, “=”, 2)
name = ReplaceAll(name, “”"", “”)
If name.Trim = “” Then Continue For i
Dim j As Integer
Dim nm As String = name
Do Until Not form.HasKey(nm)
j = j + 1
nm = name + Str(j)
Loop
If CountFields(line, “;”) < 3 Then 'form field
form.Value(nm) = NthField(elements(i), CRLF + CRLF, 2)
Else 'file field
Dim filename As String = NthField(line, “;”, 3)
filename = NthField(filename, “=”, 2)
filename = ReplaceAll(filename, “”"", “”)
If filename.Trim = “” Then filename = name
Dim tmp As FolderItem = SpecialFolder.Temporary.Child(filename)
Dim bs As BinaryStream = BinaryStream.Create(tmp, True)
Dim filedata As MemoryBlock = elements(i)
Dim t As Integer = InStr(filedata, CRLF + CRLF) + 3
filedata = filedata.StringValue(t, filedata.Size - t - 2)
bs.Write(filedata)
bs.Close
form.Value(nm) = tmp
End If
Next

Return form

End Function[/code]