Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
823 views
in Technique[技术] by (71.8m points)

c# - Xunit- Add body to a HttpWebRequest

I want to make tests to my Api so I need to test a PUT endpoint. I already tested all my Get endpoints so until now I never needed to send a body in my request. How can I do it?

TEST

public void TestUpdateSinglePlayerStats()
        {
            string id = "2019";
            HttpWebRequest request = (HttpWebRequest)WebRequest
                                        .Create("http://localhost:5000/GameStats/Update/" + id);
            request.Headers.Add("Authorization", "Bearer " + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6IjIwMTkiLCJyb2xlIjoiVXNlciIsIm5iZiI6MTYxMDEyMzY2OSwiZXhwIjoxNjEwMjEwMDY5LCJpYXQiOjE2MTAxMjM2Njl9.Dd2wzUJ5LnPBw0CbDXZZTIiQLX8074F_E1wW-qBPQzw");
            request.ContentType = "application/json";
            request.Method = "PUT";


            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
           

        }

MY API ENDPOINT

enter image description here

Tried this

      string id = "2019";
            StatUpdateModel info = new StatUpdateModel();
            info.Score = 4000;
            info.Round = 3;
            info.MonstersKilled = 102;
            info.GameMode = "Singleplayer";

            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] byte1 = encoding.GetBytes(info);

            HttpWebRequest request = (HttpWebRequest)WebRequest
                                        .Create("http://localhost:5000/GameStats/Update/" + id);
            request.Headers.Add("Authorization", "Bearer " + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6IjIwMTkiLCJyb2xlIjoiVXNlciIsIm5iZiI6MTYxMDEyMzY2OSwiZXhwIjoxNjEwMjEwMDY5LCJpYXQiOjE2MTAxMjM2Njl9.Dd2wzUJ5LnPBw0CbDXZZTIiQLX8074F_E1wW-qBPQzw");
            request.ContentType = "application/json";
            request.Method = "PUT";
            request.ContentLength = byte1.Length;
            Stream stream = request.GetRequestStream();
            stream.Write(byte1, 0, byte1.Length);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the approach suggested in the referenced answer and serialize your object to a string containing json.

Depending on the version of .NET you can do something like this (.NET Framework 4.5+ and Newtownsoft.Json NuGet package):

// ...
ASCIIEncoding encoding = new ASCIIEncoding();
string infoString = JsonConvert.SerializeObject(info);
byte[] byte1 = encoding.GetBytes(infoString);
// ...

Or for .NET Core 3.1 or .NET 5.0:

string infoString = JsonSerializer.Serialize(info);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...