У меня возникли трудности с отправкой многочастной формы через консольное приложение 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 Хост: api.bamboohr.com Content-Type: multipart/form-data; граница = ---- BambooHR-MULTIPART-мим-Boundary ---- Content-Length: 520
------ BambooHR-MULTIPART-мим-Boundary ---- Content-Disposition: form-data; name= "категория"
112 ------ BambooHR-MULTIPART-мим-Boundary ---- Content-Disposition: form-data; name= "имя_файла"
readme.txt ------ BambooHR-MULTIPART-мим-Boundary ---- Content-Disposition: form-data; name= "доля"
да ------ BambooHR-MULTIPART-мим-Boundary ---- Content-Disposition: form-data; name= "файл"; имя файла = "readme.txt" Content-Type: text/plain
Это пример текстового файла.
------ BambooHR-MULTIPART-мим-Граница ------