通过VB.NET控制台应用程序向BambooHR API提交多部分表单我遇到了很多困难 . 我发布了当前代码以及下面文档中的示例请求,当我运行此代码时,我得到(400)错误请求 . 我知道代码很乱,但我一直试图让它工作 .
我能够通过使用他们的示例代码来使GET请求工作,但他们没有任何代码来执行此特定的API调用(上传员工文件) .
任何帮助,将不胜感激 .
这是我的代码:
Sub Main()
upload(id, "https://api.bamboohr.com/api/gateway.php/company")
Console.WriteLine()
Console.WriteLine("Press ENTER to quit")
Console.ReadLine()
End Sub
Function upload(ByVal employeeId As Integer, ByVal baseUrl As String)
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 Or SecurityProtocolType.Ssl3
Dim boundary = "----BambooHR-MultiPart-Mime-Boundary----"
Dim url = String.Format("{0}/v1/employees/{1}/files/", baseUrl, employeeId)
Dim request As HttpWebRequest = WebRequest.Create(url)
request.KeepAlive = True
request.Method = "POST"
request.ContentType = "multipart/form-data; boundary=" + boundary
'Authorization is just the api key and a random string, in this case is x
'
Dim authInfo As String = api_key + ":" + "x"
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo))
request.Headers("Authorization") = "Basic " + authInfo
Dim memStream As New MemoryStream()
WriteMPF(memStream)
request.ContentLength = memStream.Length
Using requestStream = request.GetRequestStream()
memStream.Position = 0
Dim tempBuffer As Byte() = New Byte(memStream.Length - 1) {}
memStream.Read(tempBuffer, 0, tempBuffer.Length)
memStream.Close()
requestStream.Write(tempBuffer, 0, tempBuffer.Length)
End Using
Dim webresponse As HttpWebResponse = request.GetResponse()
Return webresponse
End Function
Private Sub WriteMPF(s As Stream)
WriteToStream(s, "POST /api/gateway.php/company/v1/employees/id/files/ HTTP/1.0")
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, "Host: api.bamboohr.com")
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, "Content-Type: multipart/form-data; boundary=----BambooHR-MultiPart-Mime-Boundary----")
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, "Content-Length: 520")
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, "------BambooHR-MultiPart-Mime-Boundary----")
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, "Content-Disposition: form-data; name=""category""")
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, "14")
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, "------BambooHR-MultiPart-Mime-Boundary----")
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, "Content-Disposition: form-data; name=""fileName""")
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, "test.txt")
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, "------BambooHR-MultiPart-Mime-Boundary----")
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, "Content-Disposition: form-data; name=""share""")
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, "no")
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, "------BambooHR-MultiPart-Mime-Boundary----")
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, "Content-Disposition: form-data; name=""file""; filename = ""test.txt""")
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, "Content-Type: text/plain")
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, "this is a test!")
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, vbCr & vbLf)
WriteToStream(s, "------BambooHR-MultiPart-Mime-Boundary------")
WriteToStream(s, vbCr & vbLf)
End Sub
Private Sub WriteToStream(s As Stream, txt As String)
Dim bytes As Byte() = Encoding.UTF8.GetBytes(txt)
s.Write(bytes, 0, bytes.Length)
End Sub
以下是文档中的示例请求:(链接:https://www.bamboohr.com/api/documentation/employees.php向下滚动到"Upload an Employee File")
POST /api/gateway.php/sample/v1/employees/1/files/ HTTP / 1.0主机:api.bamboohr.com内容类型:multipart / form-data; boundary = ---- BambooHR-MultiPart-Mime-Boundary ---- Content-Length:520
------ BambooHR-MultiPart-Mime-Boundary ---- Content-Disposition:form-data; NAME = “类别”
112 ------ BambooHR-MultiPart-Mime-Boundary ---- Content-Disposition:form-data; NAME = “文件名”
readme.txt ------ BambooHR-MultiPart-Mime-Boundary ---- Content-Disposition:form-data; NAME = “分享”
是的------ BambooHR-MultiPart-Mime-Boundary ---- Content-Disposition:form-data; NAME = “文件”; filename =“readme.txt”Content-Type:text / plain
这是一个示例文本文件 .
------ BambooHR-多部分MIME-边界------
2 回答
在他们的GitHub上使用php示例并将其复制到VB.NET . 这有点乱,但它确实有效 . 这是相关代码:
所需的其余代码可以在他们的GitHub上找到https://github.com/BambooHR
我怀疑至少你的_1143833会错 . 该内容长度仅适用于他们的示例 .
无论如何,我很长一段时间没有编写过VB.Net,但是通过快速测试,这个代码的修改版本可以对付我的一个REST服务,所以它应该适用于你的情况,可能还有一些小的调整 .
我的测试控制台项目使用.Net 4.6.1,但可能会与早期的.Net框架一起运行 .
在不相关的注释中,您也可以使用
vbCrLf
而不是vbCr & vbLf
.