Context
In my company we have a API that's very tricky to handle. I managed to make a successful PUT Request using Postman and now I want to build this same http request in C# using a simple Console application.
Here is the postman request:
The 2nd key has to be named exactly like that. The entry Json I can use via file or directly as value.
Here are the headers:
Only important one is the Authorization Header.
The problem
I don't know how to actually create this complicated request in C# since I'm very new to this language and couldn't find a solution to my specific problem.
I tried with the normal httpclient from C# and RestSharp but wasn't able to make this request.
Here is what I have so far:
{
class Program
{
static readonly HttpClient client = new HttpClient();
static async Task Main(string[] args)
{
using var multipart = new MultipartFormDataContent();
var jsonBytes = JsonSerializer.SerializeToUtf8Bytes(new { Metadata = "abc" });
// Need to add my json file or the json direct here somewhere
// This is how the JSON looks like
/*
{
"values": {
"z1D_WorklogDetails": "very new workinfo 3",
"z1D_View_Access": "Internal",
"z1D Action": "MODIFY",
"z2AF_Act_Attachment_1": "UID Liste.xlsx"
}
}
*/
multipart.Add(new ByteArrayContent(jsonBytes), "entry");
using var fs = File.OpenRead(@"C:\myFile.txt");
multipart.Add(new StreamContent(fs), "attach-z2AF_Act_Attachment_1");
multipart.Headers.Add("Authorization", "//my token here");
using var resp = await client.PostAsync("**/entry/HPD:IncidentInterface/INC000001479529|INC000001479529", multipart);
resp.EnsureSuccessStatusCode();
}
}
}
So how can I make this complicated request like the on shown in Postman exactly the same in C#? The API Admins told me the attachment in attach-z2AF_Act_Attachment_1 has to come Base64 encrypted
For anyone that is interested what this call actually does:
It adds a new Worklog to an existing ticket in our ticket system (BMC Remedy) and also adds an attachment to this new worklog entry.
Thank you very much.
