向Bamboo API发送多部分表单的POST请求

11

我正在通过VB.NET控制台应用程序向BambooHR API提交一个多部分表单时遇到了很大的困难。我已经发布了我的当前代码以及来自文档的示例请求,当我运行此代码时,我收到(400)Bad Request错误。我知道这段代码很混乱,但我一直在尝试让它工作。

我能够使用他们的示例代码使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,滚动到“上传员工文件”)

POST /api/gateway.php/sample/v1/employees/1/files/ HTTP/1.0 Host: api.bamboohr.com Content-Type: multipart/form-data; boundary=----BambooHR-MultiPart-Mime-Boundary---- Content-Length: 520

------BambooHR-MultiPart-Mime-Boundary---- Content-Disposition: form-data; name="category"

112 ------BambooHR-MultiPart-Mime-Boundary---- Content-Disposition: form-data; name="fileName"

readme.txt ------BambooHR-MultiPart-Mime-Boundary---- Content-Disposition: form-data; name="share"

yes ------BambooHR-MultiPart-Mime-Boundary---- Content-Disposition: form-data; name="file"; filename="readme.txt" Content-Type: text/plain

这是一个示例文本文件。

------BambooHR-MultiPart-Mime-Boundary------


(400)错误请求在他们的文档中没有提到。也许你应该联系他们并询问这是什么意思。 - user4864425
这里确实有写:https://www.bamboohr.com/api/documentation/ - obl
你不应该自己构建multipart/form-data(RFC 1867),因为有很多需要注意的事情。现在使用.NET 4.5应该非常容易。请查看此链接:https://dev59.com/oGQn5IYBdhLWcg3w_LNH - Simon Mourier
2个回答

3

我使用了他们在GitHub上的php示例,并将其复制到VB.NET中。它有点凌乱,但它可以工作。这是相关代码:

Public Function sendRequestMPF(ByVal req As BambooHTTPRequest, ByVal fileLocation As String) As BambooHTTPResponse
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 Or SecurityProtocolType.Ssl3

        Dim request As HttpWebRequest = WebRequest.Create(req.url)
        request.Method = req.method
        request.Host = "api.bamboohr.com"

        Dim boundary = "----BambooHR-MultiPart-Mime-Boundary----"

        Try
            request.ContentType = "multipart/form-data; boundary=" + boundary
            request.ContentLength = req.contents.Length
        Catch ex As Exception

        End Try

        Dim iCount As Integer = req.headers.Count
        Dim key As String
        Dim keyvalue As String

        Dim i As Integer
        For i = 0 To iCount - 1
            key = req.headers.Keys(i)
            keyvalue = req.headers(i)
            request.Headers.Add(key, keyvalue)
        Next

        Dim enc As System.Text.UTF8Encoding = New System.Text.UTF8Encoding()
        Dim bytes() As Byte = {}
        Dim pdfBytes() As Byte = {}
        Dim lBytes() As Byte = {}

        Dim fBytes() As Byte = {}
        Dim s As New MemoryStream()

        If (req.contents.Length > 0) Then
            bytes = enc.GetBytes(req.contents)
            s.Write(bytes, 0, bytes.Length)

            pdfBytes = File.ReadAllBytes(fileLocation)
            s.Write(pdfBytes, 0, pdfBytes.Length)

            Dim postHeader = vbCrLf + vbCrLf + "--" + boundary + "--" + vbCrLf
            Dim postHeaderBytes() As Byte = enc.GetBytes(postHeader)
            lBytes = enc.GetBytes(postHeader)
            s.Write(postHeaderBytes, 0, postHeaderBytes.Length)

            fBytes = s.ToArray()
            request.ContentLength = fBytes.Length
        End If

        request.AllowAutoRedirect = False

        If Not basicAuthUsername.Equals("") Then
            Dim authInfo As String = basicAuthUsername + ":" + basicAuthPassword
            authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo))
            request.Headers("Authorization") = "Basic " + authInfo
        End If

        If req.contents.Length > 0 Then
            Dim outBound As Stream = request.GetRequestStream()
            outBound.Write(fBytes, 0, fBytes.Length)
        End If

        Dim resp As BambooHTTPResponse
        Try
            Dim webresponse As HttpWebResponse = request.GetResponse()
            resp = New BambooHTTPResponse(webresponse)
            resp.responseCode = webresponse.StatusCode
            resp.headers = webresponse.Headers
        Catch e As WebException
            Console.WriteLine(e.Message)
            If (e.Status = WebExceptionStatus.ProtocolError) Then
                resp = New BambooHTTPResponse(DirectCast(e.Response, HttpWebResponse).StatusCode)
            Else
                resp = New BambooHTTPResponse(0)
            End If
        End Try

        Return resp
    End Function

Public Function buildMultiPart(ByVal params As NameValueCollection, ByVal boundary As String, ByVal contentType As String, ByVal name As String, ByVal fileName As String)
        Dim data = ""

        For Each key In params.AllKeys
            data += "--" + boundary + vbCrLf
            data += "Content-Disposition: form-data; name=""" + key + """"
            data += vbCrLf + vbCrLf
            data += params(key) + vbCrLf
        Next

        data += "--" + boundary + vbCr + vbLf
        data += "Content-Disposition: form-data; name=""" + name + """;" + " filename=""" + fileName + """" + vbCrLf
        data += "Content-Type: " + contentType + vbCrLf
        data += vbCrLf
        'data += fileData + vbCrLf + vbCrLf
        'data += "--" + boundary + "--" + vbCrLf

        Return data
    End Function

    Public Function uploadEmployeeFile(ByVal employeeId As Integer, ByVal fileName As String, ByVal fileLocation As String)
        Dim request As New BambooHTTPRequest()
        request.url = String.Format("{0}/v1/employees/{1}/files/", Me.baseUrl, employeeId)
        request.method = "POST"

        Dim boundary = "----BambooHR-MultiPart-Mime-Boundary----"

        Dim params = New NameValueCollection
        params.Add("category", "13")
        params.Add("fileName", fileName)
        params.Add("share", "no")

        request.contents = buildMultiPart(params, boundary, "application/pdf", "file", fileName)

        Return http.sendRequestMPF(request, fileLocation)
    End Function

其余所需的代码可以在他们的GitHub上找到 https://github.com/BambooHR


2
我怀疑你的Content-Length: 520至少是错误的。那个内容长度仅适用于他们的示例。
无论如何,我已经很久没有写VB.Net了,但从一个快速测试中,这段代码的修改版本可以针对我的REST服务工作,因此在您的情况下应该可以工作,可能需要进行一些微调。
我的测试控制台项目使用的是.NET 4.6.1,但可能会在较早的.NET Framework下运行。
Imports System.IO
Imports System.Net.Http

Module Module1

    Sub Main()
        Call UploadFileToWebsite(14, "no", "D:\Temp\file.pdf")
        Console.WriteLine("Please wait for a response from the server and then press a key to continue.")
        Console.ReadKey()
    End Sub

    Public Sub UploadFileToWebsite(category As Integer, share As String, file As String)
        Dim message = New HttpRequestMessage()
        Dim content = New MultipartFormDataContent()

        content.Add(New StringContent(category.ToString()), "category")
        content.Add(New StringContent(share), "share")

        Dim filestream = New FileStream(file, FileMode.Open)
        Dim fileName = System.IO.Path.GetFileName(file)

        content.Add(New StreamContent(filestream), "file", fileName)

        message.Method = HttpMethod.Post
        message.Content = content
        message.RequestUri = New Uri("https://api.bamboohr.com/api/gateway.php/company")

        Dim client = New HttpClient()
        client.SendAsync(message).ContinueWith(
            Sub(task)
                'do something with response
                If task.Result.IsSuccessStatusCode Then
                    Console.WriteLine("Uploaded OK.")
                Else
                    Console.WriteLine("Upload Failed.")
                End If
            End Sub)
    End Sub
End Module

另外一件无关的事是,你也可以使用 vbCrLf 替代 vbCr & vbLf


是的,您还需要添加正常的授权。多部分表单并没有什么特别之处,它只是一种在标准表单数据中包含文件的约定,以便在接收端可以将所有内容拆分开来。我没有网站授权,所以无法调查错误,但我建议您在If task..行上设置一个断点,并检查异常情况。 - K Scandrett
从BambooHR网站上看,URI请求的形式似乎可以是https://{API Key}:x@api.bamboohr.com/api/gateway.php/{subdomain}/v1/employees/... - K Scandrett
这可能有助于调试 https://dev59.com/12gu5IYBdhLWcg3wuJSF#11122121 - K Scandrett
你有取得进展吗? - K Scandrett
1
嗨K,我最近有点忙没时间处理这个问题。我授予你奖励只是因为你一直在尝试帮助我,等我有时间再处理这个问题时我会通知你的,谢谢! - obl
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接