博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
simulate POST request
阅读量:4031 次
发布时间:2019-05-24

本文共 7588 字,大约阅读时间需要 25 分钟。

15
4

I'm testing on Windows, trying to simulate POST requests (with different form variables) for load testing. I have tried all kinds of load testing software but failed to get it working.

For GET requests, I know I can just put parameters behind the url

But how do I simulate a POST request?

I have a chrome REST Client but I do not know what to put in the headers and data.

Here's what I've tried so far:

class Program    {        static void Main(string[] args)        {            string viewstateid = "/wEPDwUKLTY3NjEyMzE4NWRkK4DxZpjTmZg/RGCS2s13vkEWmwWiEE6v+XrYoWVuxeg=";            string eventid ="/wEdAAoSjOGPZYAAeKGjkZOhQ+aKHfOfr91+YI2XVhP1c/pGR96FYSfo5JULYVvfQ61/Uw4pNGL67qcLo0vAZTfi8zd7jfuWZzOhk6V/gFA/hhJU2fx7PQKw+iST15SoB1LqJ4UpaL7786dp6laCBt9ubQNrfzeO+rrTK8MaO2KNxeFaDhrQ0hxxv9lBZnM1SHtoODXsNUYlOeO/kawcn9fX0BpWN7Brh7U3BIQTZwMNkOzIy+rv+Sj8XkEEA9HaBwlaEjg=";            string username = "user1";            string password = "ttee";            string loginbutton = "Log In";            string URLAuth = "http://localhost/login.aspx";            string postString = string.Format("VIEWSTATE={0}&EVENTVALIDATION={1}&LoginUser_UserName={2}&LoginUser_Password={3}&LoginUser_LoginButton={4}",viewstateid,eventid, username, password,realm,otp,loginbutton);            const string contentType = "application/x-www-form-urlencoded";            System.Net.ServicePointManager.Expect100Continue = false;            CookieContainer cookies = new CookieContainer();            HttpWebRequest webRequest = WebRequest.Create(URLAuth) as HttpWebRequest;            webRequest.Method = "POST";            webRequest.ContentType = contentType;            webRequest.CookieContainer = cookies;            webRequest.ContentLength = postString.Length;            webRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";            webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";           webRequest.Referer = "http://localhost/login.aspx";            StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());            requestWriter.Write(postString);            requestWriter.Close();            StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());            string responseData = responseReader.ReadToEnd();            Console.WriteLine(responseData);            responseReader.Close();            webRequest.GetResponse().Close();        }    }
 

4 Answers

19

You can try  which is easy to test APIs.

 
 
How do I post to an internal only running development server with this? –   
Mar 2 at 7:55
 
I'm not sure you can. If you want to test a development API I would suggest using Postman chrome app or if you want to test your API automatically you would have to use some automation tool (for example for PHP you could use Behat) –   
Mar 2 at 9:21
 
Exactly my point. –   
Mar 2 at 10:15
8

It would be helpful if you provided more information - e.g. what OS your using, what you want to accomplish, etc. But, generally speaking cURL is a very powerful command-line tool I frequently use (in linux) for imitating HTML requests:

For example:

curl --data "post1=value1&post2=value2&etc=valetc" http://host/resource

OR, for a RESTful API:

curl -X POST -d @file http://host/resource

You can check out more information here-> 


EDITs:

OK. So basically you're looking to stress test your REST server? Then cURL really isn't helpful unless you want to write your own load-testing program, even then sockets would be the way to go. I would suggest you check out . The Gatling documentation explains how to set up the tool, and from there your can run all kinds of GET, POST, PUT and DELETE requests. 

Unfortunately, short of writing your own program - i.e. spawning a whole bunch of threads and inundating your REST server with different types of requests - you really have to rely on a stress/load-testing toolkit. Just using a REST client to send requests isn't going to put much stress on your server.


More EDITs

So in order to simulate a post request on a socket, you basically have to build the initial socket connection with the server. I am not a C# guy, so I can't tell you exactly how to do that; I'm sure there are 1001 C# socket tutorials on the web. With most RESTful APIs you usually need to provide a URI to tell the server what to do. For example, let's say your API manages a library, and you are using a POST request to tell the server to update information about a book with an id of '34'. Your URI might be 

http://localhost/library/book/34

Therefore, you should open a connection to localhost on port 80 (or 8080, or whatever port your server is on), and pass along an HTML request header. Going with the library example above, your request header might look as follows:

POST library/book/34 HTTP/1.0\r\nX-Requested-With: XMLHttpRequest\r\nContent-Type: text/html\r\nReferer: localhost\r\nContent-length: 36\r\n\r\ntitle=Learning+REST&author=Some+Name

From here, the server should shoot back a response header, followed by whatever the API is programed to tell the client - usually something to say the POST succeeded or failed. To stress test your API, you should essentially do this over and over again by creating a threaded process.

Also, if you are posting JSON data, you will have to alter your header and content accordingly. Frankly, if you are looking to do this quick and clean, I would suggest using python (or perl) which has several libraries for creating POST, PUT, GET and DELETE request, as well as POSTing and PUTing JSON data. Otherwise, you might end up doing more programming than stress testing. Hope this helps!

 
 
using windows OS, i want to simulate data POSTS for load test, I have tried all kinds of load test, but failed to get it working –   
Sep 1 '13 at 9:35
 
for localhost it doesn't seem to work –   
Sep 1 '13 at 9:55
 
would really like to write own program, the question is I am still unable to put the POST parameters successfully in the webrequest in my C# code –   
Sep 1 '13 at 10:34
 
Well that I might be able to help you with. Essentially, after you open a socket to the url specified in the definitions for your rest server, you will have to construct a http request header with the information. It would help to know what framework, etc. your REST server is built with. And, what type of data do you need to pass? JSON? Or plain text? I would be happy to help however I can, maybe we can use SO's chat feature? – 
Sep 1 '13 at 10:44
 
asp .net framework 4, I just want to pass the parameters into the webpage to simulate 50 POST requests/sec with different parameters –   
Sep 1 '13 at 12:14 
4

Postman is the best application to test your APIs !

You can import or export your routes and let him remember all your body requests ! :)


 
1

Simple way is to use curl from command-line, for example:

DATA="foo=bar&baz=qux"curl --data "$DATA" --request POST --header "Content-Type:application/x-www-form-urlencoded" http://example.com/api/callback | python -m json.tool

or here is example how to send raw POST request using Bash shell (JSON request):

exec 3<> /dev/tcp/example.com/80DATA='{"email": "foo@example.com"}'LEN=$(printf "$DATA" | wc -c)cat >&3 << EOFPOST /api/retrieveInfo HTTP/1.1Host: example.comUser-Agent: BashAccept: */*Content-Type:application/jsonContent-Length: $LENConnection: close$DATAEOF# Read response.while read line <&3; do   echo $linedone

转载地址:http://yuebi.baihongyu.com/

你可能感兴趣的文章
C语言位扩展
查看>>
linux dump_backtrace
查看>>
linux irqdebug
查看>>
git 常用命令
查看>>
linux位操作API
查看>>
snprintf 函数用法
查看>>
uboot.lds文件分析
查看>>
uboot start.s文件分析
查看>>
没有路由器的情况下,开发板,虚拟机Ubuntu,win10主机,三者也可以ping通
查看>>
本地服务方式搭建etcd集群
查看>>
安装k8s Master高可用集群
查看>>
忽略图片透明区域的事件(Flex)
查看>>
忽略图片透明区域的事件(Flex)
查看>>
AS3 Flex基础知识100条
查看>>
Flex动态获取flash资源库文件
查看>>
flex中设置Label标签文字的自动换行
查看>>
Flex 中的元数据标签
查看>>
flex4 中创建自定义弹出窗口
查看>>
01Java基础语法-11. 数据类型之间的转换
查看>>
01Java基础语法-13. if分支语句的灵活使用
查看>>