SoapUI 设置 request data with json body
--背景
使用WCF定义REST风格的WebService,如下:
[ServiceContract]
public interface INISTService
{
[OperationContract, WebInvoke(UriTemplate = "/EnrollTP/{context}",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare)]
bool Enroll(string packageStr, string context);
}
--编写测试代码
由于参数“packageStr”的内容过大超过URI的限定长度,需要将其放入Request对象中,在自己编写客户端代码测试时,将packageStr的值转为BASE64STRING放入参数sendData中,并将其存入到HTTPWebRequest对象中。
HttpWebRequest webRequest = HttpWebRequest.Create(uri) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.Accept = AcceptHeader;
webRequest.ContentType = ContentType;
if (timeOut != 0)
webRequest.Timeout = timeOut;
Stream stream = webRequest.GetRequestStream();
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write(sendData);
}
--使用SoapUI时,按照接口定义设置request data为JSON类型(copy BASE64STRING into),如图