0

I am using Playwright with C# and I need to send a POST request. Since I need to include a CSV data file in the payload, I am using multipart encoding, but have not had much luck so far. The CSV file lives in a folder in my VS project called TestData.

I am pretty sure the issue is with the Buffer. I have gotten a 200 response with no CSV data, so at least I know the API is working. Currently I am getting a 400 response with this code:

 public async Task<string> CreateNewSimulation()
        {
            await CreateSimulatorRequestContext();
            var csvFilePath = @"TestData\VT_Gateway.csv";
            var csvFileContent = File.ReadAllText(csvFilePath);
            var csvFileName = Path.GetFileName(csvFilePath);
            var file = new FilePayload()
            {
                Name = csvFileName,
                MimeType = "text/csv",
                Buffer = System.Text.Encoding.UTF8.GetBytes(csvFileContent)
            };
            var multipart = Request.CreateFormData();
            multipart.Set("files", file);
            multipart.Set("ReplayDelayMilliseconds", 3000);
            multipart.Set("Limit", 142);
            var simResponse = await Request.PostAsync("/api/devicesimulator/DeviceSimulator", new() { Multipart = multipart });
            NEW_SIM_MSG = simResponse.ToString();
            return NEW_SIM_MSG;
        }

These are the headers I am sending with the request:

 var headers = new Dictionary<string, string>
            {
                { "Content-Type", "multipart/form-data" },
                { "Authorization", "Bearer " + ACCESS_TOKEN },
                { "Ocp-Apim-Subscription-Key", OCP_KEY }
            };
1
  • Did you manage to resolved this issue? I'm getting the same issue.
    – stwoo
    Nov 17 at 21:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.