0

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);
Fábio Pires
  • 89
  • 1
  • 2
  • 10
  • Does this answer your question? [Setting a WebRequest's body data](https://stackoverflow.com/questions/4256136/setting-a-webrequests-body-data) – serge.karalenka Jan 08 '21 at 17:35
  • Tried that but I am getting the error saying that cannot convert from my model to char[](edited my question) – Fábio Pires Jan 08 '21 at 18:32

1 Answers1

0

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);
serge.karalenka
  • 980
  • 1
  • 15
  • 29