I have this problem. i have to submit a file (or not) to an endpoint on an API of bmc.
the KEY:entry with the VALUE:data_entry.txt is the json to send with the values, as the same of the body.The attach-z2AF_WIAttachment1 is the file i want to submit. I'm it's always throuwing some error, or headers invalid, or filetype not valid, but in postman is working.
I cant convert to C#.this is my code so far, or now.
try { //authentication var dict = new Dictionary<string, string>(); dict.Add("username", "applicationUsernameJonDoe"); dict.Add("password", "applicationPassowrdXPTO"); var clientLogin = new HttpClient(); var req = new HttpRequestMessage(HttpMethod.Post, Endpoint_loginITSM) { Content = new FormUrlEncodedContent(dict) }; var res = clientLogin.SendAsync(req); //.Result.ToString(); var body = res.GetAwaiter().GetResult().Content.ReadAsStringAsync(); //pedido de criação de registo using (var client = new HttpClient()) { client.Timeout = TimeSpan.FromMinutes(10); var request = new HttpRequestMessage { RequestUri = new Uri(Endpoint_CreateITSM), Method = HttpMethod.Post }; request.Headers.Add("Authorization", body.Result.ToString()); if (!string.IsNullOrEmpty(registos.Objeto.fileName)) { registos.Objeto.Registo.z2AF_WIAttachment1 = registos.Objeto.fileName; } string json = JsonConvert.SerializeObject(new { values = registos.Objeto }); byte[] file_bytes = System.Convert.FromBase64String(registos.Objeto.fileEncoded); MemoryStream memoryStream = new MemoryStream(); using (BsonDataWriter writer = new BsonDataWriter(memoryStream)) { JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(writer, registos.Objeto.Registo); } var data_entry_bytes = memoryStream.ToArray(); // we need to send a request with multipart/form-data var multiForm = new MultipartFormDataContent(); ByteArrayContent data_entry_json_content = new ByteArrayContent(data_entry_bytes); data_entry_json_content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); data_entry_json_content.Headers.ContentDisposition = new ContentDispositionHeaderValue("entry") { FileName = "data_entry.txt", Name = "entry", }; multiForm.Add(data_entry_json_content); ByteArrayContent z2AF_WIAttachment1_content = new ByteArrayContent(file_bytes); z2AF_WIAttachment1_content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); z2AF_WIAttachment1_content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attach-z2AF_WIAttachment1") { FileName = registos.Objeto.fileName, Name = "attach-z2AF_WIAttachment1", }; multiForm.Add(z2AF_WIAttachment1_content); request.Content = multiForm; var result = await client.SendAsync(request); var resBody = result.Content.ReadAsStringAsync().Result.ToString();//.ConfigureAwait(false); dynamic _resBody = JsonConvert.DeserializeObject<dynamic>(resBody); string registoID = _resBody["values"].SysRequestID; return ResponseHandler<string>.Resposta(false, "resposta api bit criar registos", registoID); } } catch (Exception e) { string classname = this.GetType().Name; CentralLibrary.Services.ErrorLoggingService.ErrorLogsForCore(classname, e, _env.WebRootPath); return ResponseHandler<string>.Resposta(true, "EXCEPTION : resposta api bit criar registos", e.Message); }