locked
Send MultipartForm Data in VS 2017 Web Performance Test RRS feed

  • Question

  • So we have a web api that accepts an image from the client in the format of a Multipartformdata post. However, I'm not that familiar with the WebTestRequest object. 

    Can someone point me to some code that would allow me to do this using the WebTestRequest object? This is what I have so far.

    var webRequest = new WebTestRequest($"http://someApi.azurewebsites.net/api/v1/00000000-0000-0000-0000-00/Sync/UploadPhoto/2e53e903-ea2f-4a9c-a7b9-ef4ed841ad99");
                
                webRequest.Method = "POST";
                webRequest.Headers.Add("Content-Type", "application/octet-stream");
                var formDataStream = new MemoryStream();
                var boundary = $"----------{Guid.NewGuid()}";
                string header = string.Format("--{0}\r\nContent-Dis-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n",
                                                    boundary,
                                                    "file",
                                                    "f8017498-0c9b-41ec-904f-e3e47c776148",
                                                    "application/octet-stream"
                                              );
    
                formDataStream.Write(Encoding.UTF8.GetBytes(header), 0, Encoding.UTF8.GetByteCount(header));
                // write the file data directly to the stream, rather than serialization
                byte[] fileData = File.ReadAllBytes(@"C:\Users\user\Pictures\f8017498-0c9b-41ec-904f-e3e47c776148");
                Console.WriteLine($"Content Length {fileData.Length}");
                formDataStream.Write(fileData, 0, fileData.Length);
                webRequest.Headers.Add("Content-Length", fileData.Length.ToString());
                string footer = "\r\n--" + boundary + "--\r\n";
                formDataStream.Write(Encoding.UTF8.GetBytes(footer), 0, Encoding.UTF8.GetByteCount(footer));
    
                formDataStream.Position = 0;
                FormPostHttpBody body = new FormPostHttpBody();
                body.WriteHttpBody(webRequest, formDataStream);
                
                webRequest.Body = body;
                formDataStream.Close();
                yield return webRequest;
                webRequest = null;

    Wednesday, December 26, 2018 6:10 PM

All replies

  • Hi stillCodingBadly,

    I know that you used the coded web performance test in your side, but whether you test it with the web test record or Fiddler tool to record a web performance test in your side, whether it could really capture the request before you custom the C# code?

    In addition, my understanding is that the coded web performance test would be similar how you handle

    posting multipartform-data Using .NET/C# WebRequest, so you could also research some related documents in your side with this title, maybe you could get much more useful information.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 4, 2019 3:00 AM