32
29

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

curlコマンドでGET/POSTをサクっと確認

Last updated at Posted at 2015-10-19

やりたいこと

ターミナルからGET/POSTを簡単に送信したい。
あとファイルも送信したい。

(その1)GET

送り側

send.sh
# !/bin/bash

# 受け側のURL
url="http://example.com/cgi-bin/recv.php?get1=get1_val&get2=get2_val"

# 実行
curl $url

受け側($_GET) ※HTTPサーバを立てるのが面倒な場合は、一番下の「備考」を参照

recv.php
<?php
var_dump($_GET);      

実行

$ ./send.sh
array(2) {
  'get1' =>
  string(8) "get1_val"
  'get2' =>
  string(8) "get2_val" 
}

(その2)POST

送り側

send.sh
# !/bin/bash
url="http://example.com/cgi-bin/recv.php"
curl -F 'post1=post1_val'  -F 'post2=post2_val' $url 

受け側($_POST)

recv.php
<?php
var_dump($_POST);      

実行

$ send.sh
array(2) {
  'post1' =>
  string(9) "post1_val"
  'post2' =>
  string(9) "post2_val"  
}

(その3)POST(ファイル送信)

送り側(@を付ける)

send.sh
# !/bin/bash
url="http://example.com/cgi-bin/recv.php"
curl -F 'post=@/tmp/a.jpg' $url

受け側($_POST)

recv.php
<?php
var_dump($_FILES);   

実行

$ ./send.sh
array(1) {
  'post' =>
  array(5) {
    'name' =>
    string(5) "a.jpg"
    'type' =>
    string(10) "image/jpeg"
    'tmp_name' =>
    string(14) "/tmp/phpvNLvGY"
    'error' =>
    int(0)
    'size' =>
    int(18136)
  }
}

curlでproxyを設定する場合は以下を参照

サーバを立てるのが面倒な場合はpythonで確認できます

32
29
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
29

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?