17
13

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 コマンドケーススタディ

Last updated at Posted at 2014-12-18

自分用メモも兼ねて。

#Q1. curl コマンドでGETパラメータがうまく渡らない

$ curl -X GET http://api.facebook.com/restserver.php?method=links.getStats&urls=http://jp.techcrunch.com/2014/12/18/20141217googles-end-to-end-email-encryption-tool-gets-closer-to-launch/

<?xml version="1.0" encoding="UTF-8"?>
<error_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd">
  <error_code>100</error_code>
  <error_msg>The parameter urls is required</error_msg> #urlパラメータが落ちてる
  <request_args list="true">
    <arg>
      <key>method</key>
      <value>links.getStats</value>
    </arg>
  </request_args>
</error_response>

#A1. & をエスケープしましょう

$ curl -X GET http://api.facebook.com/restserver.php?method=links.getStats\&urls=http://jp.techcrunch.com/2014/12/18/20141217googles-end-to-end-email-encryption-tool-gets-closer-to-launch/

<?xml version="1.0" encoding="UTF-8"?>
<links_getStats_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd" list="true">
  <link_stat>
    <url>http://jp.techcrunch.com/2014/12/18/20141217googles-end-to-end-email-encryption-tool-gets-closer-to-launch/</url>
    <normalized_url>http://jp.techcrunch.com/2014/12/18/20141217googles-end-to-end-email-encryption-tool-gets-closer-to-launch/</normalized_url>
    <share_count>25</share_count>
    <like_count>32</like_count>
    <comment_count>6</comment_count>
    <total_count>63</total_count>
    <click_count>0</click_count>
    <comments_fbid>954845757876498</comments_fbid>
    <commentsbox_count>0</commentsbox_count>
  </link_stat>
</links_getStats_response>

bash 側で & を拾ってしまったようです。

もしくは、文字列にして渡しましょう。

$ curl -G -d "method=links.getStats&urls=http://jp.techcrunch.com/2014/12/18/20141217googles-end-to-end-email-encryption-tool-gets-closer-to-launch/" http://api.facebook.com/restserver.php

オプションが -X GET ではなく -G になってるのがミソです。

オプションまとめ

オプション 内容
-X GET/POST/PUT HTTPメソッドの指定
-G GET メソッド
-A UserAgent の書き換え
-H header を指定する
-d GET/POST用のパラメータを記述
-L リクエスト先が移動してた(301/302)場合にリクエスト先に再実行する
-I レスポンスヘッダだけ取得
-o 出力先。/dev/null にすれば破棄
-s サイレント(何も出力しない)

例)

$ curl -X POST -H "Content-Type: application/json" -A "Mozilla/5.0 (Windows NT 6.1; rv:33.0) Gecko/20100101 Firefox/33.0" http://api.hogehoge.com/post/resource -d '{"paramter":"value"}'
17
13
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
17
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?