Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate HTTP requests?

How to simulate HTTP requests. For example, I want to simulate request to insert data into my database, to check safety and reliability of my program. Are there some good tools ?

like image 525
ttworkhard Avatar asked Dec 28 '25 07:12

ttworkhard


2 Answers

For *nix OS you can use telnet, curl or wget utilites:

Telnet:

user@host:~$ telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /api.php?param1=10&param2=20 HTTP/1.1   # here is your request params
Host: localhost                             # required HTTP header
                                            # double ENTER for sending request

Check telnet man-page of your OS for advanced options.

Curl:

user@host:~$ curl \
--header 'Host: localhost' \
--user-agent 'Mozilla/5.0 (Linux x86_64; rv:10.0.12) Gecko/ Firefox/10.0.12' \
--no-keepalive \
http://localhost/api.php?param1=10&param2=20

Check curl man-page of your OS for advanced options.

Wget:

user@host:~$ wget http://localhost/api.php?param1=10&param2=20

Check wget man-page of your OS for advanced options.

FYI if you choose curl then you'll allow to use power of *nix shell: grep, sed, output redirection and a lot of other useful stuff.

like image 85
Alexander Yancharuk Avatar answered Dec 30 '25 21:12

Alexander Yancharuk


If you want to try from the server side cURL is definitely the easiest way. Since you tagged this question with PHP here is a simple PHP script that simulates a POST request.

<?php
$url = "http://yoursite.com/yourscript.php";
$postdata = array (
    "name" => "Joe",
    "address" => "Some street",
    "state" => "NY",
);

$req = curl_init($url);
curl_setopt($req, CURLOPT_POST, true);
curl_setopt($req, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($req);

If you want to try requests from the browser there are very good extensions for both Chrome and Firefox. For example (for chrome) POSTMAN or Advanced REST client.

like image 32
markz Avatar answered Dec 30 '25 21:12

markz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!