Reputation: 37993
I'm writing some webtests in Visual Studio 2015 Enterprise to do a load test on my API.
Several of my API calls expect a Json object as the body of the request. But the webtest interface doesn't appear to have any way to set the body of the Post request directly; you can add keys and values, but you can't set the request, either to an object that would implicitly be serialized, or even just a plain string.
So how do you post Json in a web test?
Upvotes: 3
Views: 7483
Reputation: 1
In the web test -> string body -> Content Type property,
Content Type Picture for Json Data
Upvotes: 0
Reputation: 69
After searching around for a while I found a solution allowing you to send through a JSON object in a post request of a web test.
1) Create a web test performance plugin: https://learn.microsoft.com/en-us/visualstudio/test/how-to-create-a-web-performance-test-plug-in?view=vs-2017
2) Add the following code into the class (don't forget to build):
using System.ComponentModel;
using Microsoft.VisualStudio.TestTools.WebTesting;
namespace WebTests.RequestPlugins
{
[DisplayName("Add JSON content to Body")]
[Description("HEY! Tip! Uglify your JSON before pasting.")]
public class AddJsonContentToBody : WebTestRequestPlugin
{
[Description("Assigns the HTTP Body payload to the content provided and sets the content type to application/json")]
public string JsonContent { get; set; }
public override void PreRequest(object sender, PreRequestEventArgs e)
{
var stringBody = new StringHttpBody();
stringBody.BodyString = JsonContent;
stringBody.ContentType = "application/json";
e.Request.Body = stringBody;
}
}
3) On your request URL r-click and select 'Add Request Plugin' then you should see your new plugin.
4) Uglify your json to be able to paste.
5) Run test
Source to code: https://ericflemingblog.wordpress.com/2016/01/21/helpful-custom-request-plugins-for-web-tests/
Upvotes: 0
Reputation: 14038
There are two options, perhaps more, depending on your needs. Both of the mechanisms have the same set of properties to set the URL and many other fields.
In the web test editor you can add a web service request (the Insert web service request
context menu command) then set it StringBody
field. The contents of the string body can contain context parameters.
The context menu of a normal request has an Add file upload parameter
.
Upvotes: 9