Home > Net >  How to CREATE POST IN C# to an endpoint on BMC REMEDY API to create a ticket with files submited in
How to CREATE POST IN C# to an endpoint on BMC REMEDY API to create a ticket with files submited in

Time:08-11

I have this problem. i have to submit a file (or not) to an endpoint on an API of bmc. enter image description here

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);
        }

CodePudding user response:

I'm not entirely sure what's going wrong here. It can be a lot of things, but I might be able to get you going. The last couple of weeks I build a HttpClient that sends a file with metadata to a GraphQL endpoint.

Please ensure the following: I think you are requesting the file through an call. Please store it in a variable as a Byte[] using the ReadAsByteArrayAsync(). Do note decode it or cast it to a string or anything. You'll just corrupt the file.

var response = client.GetAsync(fileUrl);
    
var downloadedFile = await response.Result.Content.ReadAsByteArrayAsync();

The following code might not work entirely in your case, but should help you get going building the right request, since I'm also sending metadata in my request containing the file extension and some other information. This will most likely send the file to your API without a file extension.

using (var client = new HttpClient())
{
    var file = new byte[] { 1, 2, 3 };
    var fileToUpload = new ByteArrayContent(file);

    var formData = new MultipartFormDataContent
                {
                    { fileToUpload, "entry", "passInFileExtensionForExample"},
                    { fileToUpload, "attach-z2AF_WIAttachment1", "passInFileExtensionForExample" }
                };

    var response = await client.PostAsync("endpoint", formData);
}

Add the Bearer token using the following code:

 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

CodePudding user response:

So i've discover the solution for my problem. I'm gonna to submit only one file. I have to submit also the Json body as a file "entry" "data_entry.txt" and for HttpRequestMessage you have to have a content MultipartFormDataContent and here you can add as many files as you have. i have to convert the Json body to a file ( in this case i converted to binary Array) with the name entry, and the name of the file data_entry.txt, but it's what the endpoint needs, so...whatever.

  using (var client = new HttpClient())
                {
                    client.Timeout = TimeSpan.FromMinutes(10);
                    MultipartFormDataContent content = new MultipartFormDataContent();

                    //adicionar ficheiro
                    byte[] file_bytes = System.Convert.FromBase64String(registos.Objeto.fileEncoded);
                    StreamContent fileContent = new StreamContent(new MemoryStream(file_bytes));       
                    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                    {
                        Name = "files[attach-z2AF_WIAttachment1]",
                        FileName = registos.Objeto.fileName
                    };

                    fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
                    content.Add(fileContent);
                    //adicionar ficheiro entry
                    StreamContent entryStreamContent = new StreamContent(new MemoryStream(ObjectToByteArray(registos.Objeto.Registo)));
                    entryStreamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                    {
                        Name = "files[entry]",
                        FileName = "data_entry.txt"
                    };

                    entryStreamContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                    content.Add(entryStreamContent);

                    var request = new HttpRequestMessage
                    {
                        RequestUri = new Uri(Endpoint_CreateITSM),
                        Method = HttpMethod.Post,
                        Content= content

                    };

                    request.Headers.Add("Authorization", body.Result.ToString());
                    string json = JsonConvert.SerializeObject(new { values = registos.Objeto.Registo});
                    request.Content = new ByteArrayContent(Encoding.UTF8.GetBytes(json));
                    request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    var resposta = await client.SendAsync(request);
                    var respostaBody = resposta.Content.ReadAsStringAsync().Result.ToString();//.ConfigureAwait(false);
                    dynamic _respostaBody = JsonConvert.DeserializeObject<dynamic>(respostaBody);
                    string _registoID = _respostaBody["values"].SysRequestID;
                    return ResponseHandler<string>.Resposta(false, "resposta api bit criar registos", _registoID);         

So this is my solution. and it's working :)

  • Related