【问题标题】:How to do HTTP-request/call with JSON payload from command-line?如何从命令行使用 JSON 有效负载进行 HTTP 请求/调用?
【发布时间】:2011-05-17 22:31:14
【问题描述】:

从命令行进行 JSON 调用的最简单方法是什么?我有一个网站,它通过 JSON 调用来检索其他数据。

Google Chrome 中显示的 Request Payload 如下所示:

{"version": "1.1", "method":"progr","id":2,"params":{"call":...} }

这是关于从(最好是)linux 命令行进行调用并检索 JSON 内容,而不是解析传入的 JSON 数据。

【问题讨论】:

    标签: linux json command-line web-crawler


    【解决方案1】:

    你也可以使用 wget:

    wget -O- --post-data='{"some data to post..."}' \
      --header='Content-Type:application/json' \
      'http://www.example.com:9000/json'
    

    【讨论】:

      【解决方案2】:

      使用 curl,假设数据是 POST 的,类似于

      curl -X POST http://example.com/some/path -d '{"version": "1.1", "method":"progr","id":2,"params":{"call":...} }'
      

      如果您只是使用 GET 检索数据,并且不需要发送任何 URL 参数, 你只需运行curl http://example.com/some/path

      【讨论】:

      • 你可能也需要-H "Content-Type: application/json"
      • HTTP/1.1 415 不支持的媒体类型:-(
      【解决方案3】:

      您也可以将wgetpost-file 一起使用,我发现这很有用。

      wget --post-file=[file] --header=Content-Type:application/json [URL]
      

      您可以将内容保留在文件中,内容将作为post数据发送。

      【讨论】:

        【解决方案4】:
        curl --request POST \
        --url http://localhost:8099/someservice/services/boo \
        --header 'authorization: Basic dkfhsdlepwmdseA==' \
        --header 'cache-control: no-cache' \
        --header 'content-type: application/json' \
        --data '{"value": "24.127.1212.123"}'
        

        【讨论】:

          【解决方案5】:

          你看过curl吗?它非常擅长通过命令行促进 HTTP GET/POST 请求。

          例如(对于 GET 请求):

          C:\WINDOWS>curl "http://search.twitter.com/search.json?q=twitterapi&result_type=
          popular"
          {"results":[{"from_user_id_str":"32316068","profile_image_url":"http://a2.twimg.
          com/profile_images/351010682/twitblock_profile_normal.png","created_at":"Thu, 25
           Nov 2010 14:37:46 +0000","from_user":"twitblockapp","id_str":"7805146834669569"
          ,"metadata":{"result_type":"popular","recent_retweets":10},"to_user_id":null,"te
          xt":"blocking and reporting functions are currently failing. @TwitterAPI have be
          en notified. http://j.mp/id5w3m","id":7805146834669569,"from_user_id":32316068,"
          geo":null,"iso_language_code":"en","to_user_id_str":null,"source":"<a href=&q
          uot;http://twitter.com" rel="nofollow">Tweetie for Mac</a&g
          t;"}],"max_id":9607558079713280,"since_id":0,"refresh_url":"?since_id=9607558079
          713280&q=twitterapi","results_per_page":15,"page":1,"completed_in":0.012698,"sin
          ce_id_str":"0","max_id_str":"9607558079713280","query":"twitterapi"}
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-02-01
            • 2014-05-10
            • 2013-01-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多