curl perl python get 发送json数据

get 发送jason 数据

GET /library/books/_mget
{
   "ids" : [ "1", "3" ]
}



Perl 版:
[elk@node01 ~]$ perl a1.pl 
{"docs":[{"_index":"library","_type":"books","_id":"1","_version":1,"found":true,"_source":{ "title":"Elasticsearch: The Definitive Guide","price":5,"preview":"Elasticsearch is a distributed, scalable, real-time search and analytics engine. It ena‐bles you to search, analyze, and explore your data, often in ways that you did not anticipate at the start of a project. It exists because raw data sitting on a hard drive is just not useful." ,"publish_date":"2015-02-08"}},{"_index":"library","_type":"books","_id":"3","_version":1,"found":true,"_source":{ "title":"Elasticsearch Blueprints","price":9,"preview":"This book starts with the creation of a Google-like web search service, enabling you to generate your own search results. You will then learn how an e-commerce website can be built using Elasticsearch. We will discuss various approaches in getting relevant content up the results, such as relevancy based on how well a query matched the text, time-based recent documents, geographically nearer items, and other frequently used approaches." , "publish_date":"2015-06-01"}}]}[elk@node01 ~]$ cat a1.pl 
use  LWP::UserAgent;   
use LWP;  
use Encode;  
use LWP::Simple;  
use LWP::UserAgent;  
use HTTP::Cookies;  
use HTTP::Headers;  
use HTTP::Response;  
use Encode;  
use URI::Escape;  
use URI::URL;  
use JSON;  
use Data::Dumper;  
  my $ua = LWP::UserAgent->new;  
     $ua->agent("Mozilla/5.0 (Windows NT 6.1; rv:30.0) Gecko/20100101 Firefox/30.0");  
  my $cookie_jar = HTTP::Cookies->new(  
     file=>'lwp_cookies.txt',  
     autosave=>1,  
     ignore_discard=>1);  
     $ua->cookie_jar($cookie_jar);    
   my     $login_url ="http://192.168.137.2:9200/library/books/_mget";    
   my $post = {
   "ids" => [ "1", "3" ]
  };

    use JSON qw(encode_json);    
    $json_string = encode_json($post);    
    
    my $req = HTTP::Request->new(    
        'GET' => $login_url  
    );    
    $req->referer("https://wx.qq.com/?&lang=zh_CN");    
    $req->content_type('application/json; charset=UTF-8')    
      ;    #post请求,如果有发送参数,必须要有这句    
    $req->content("$json_string");    #发送post的参数    
    my $res = $ua->request($req);    
    print $res->content();            #获取的是响应正文 
[elk@node01 ~]$ perl a1.pl 
{"docs":[{"_index":"library","_type":"books","_id":"1","_version":1,"found":true,"_source":{ "title":"Elasticsearch: The Definitive Guide","price":5,"preview":"Elasticsearch is a distributed, scalable, real-time search and analytics engine. It ena‐bles you to search, analyze, and explore your data, often in ways that you did not anticipate at the start of a project. It exists because raw data sitting on a hard drive is just not useful." ,"publish_date":"2015-02-08"}},{"_index":"library","_type":"books","_id":"3","_version":1,"found":true,"_source":{ "title":"Elasticsearch Blueprints","price":9,"preview":"This book starts with the creation of a Google-like web search service, enabling you to generate your own search results. You will then learn how an e-commerce website can be built using Elasticsearch. We will discuss various approaches in getting relevant content up the results, such as relevancy based on how well a query matched the text, time-based recent documents, geographically nearer items, and other frequently used approaches." , "publish_date":"2015-06-01"}}]}[elk@node01 ~]$ 


python 版:
def get_library():
    url = 'http://192.168.137.2:9200/library/books/_mget'
    data = {"ids" : [ "1", "3" ]}
    print type(data)
    data = json.dumps(data)
    print type(data)
    request = urllib2.Request(url,data)
    request.add_header("Content-Type", "application/json")
    request.add_header("Accept", "application/json")
    request.get_method = lambda: "GET"  # "GET,POST,PUT,DELETE"
    response = urllib2.urlopen(request)
    response_txt = response.read()
    response_header = response.info()
    print response_txt
    print response_header


C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a4.py
<type 'dict'>
<type 'str'>
{"docs":[{"_index":"library","_type":"books","_id":"1","_version":1,"found":true,"_source":{ "title":"Elasticsearch: The Definitive Guide","price":5,"preview":"Elasticsearch is a distributed, scalable, real-time search and analytics engine. It ena‐bles you to search, analyze, and explore your data, often in ways that you did not anticipate at the start of a project. It exists because raw data sitting on a hard drive is just not useful." ,"publish_date":"2015-02-08"}},
{"_index":"library","_type":"books","_id":"3","_version":1,"found":true,"_source":{ "title":"Elasticsearch Blueprints","price":9,"preview":"This book starts with the creation of a Google-like web search service, enabling you to generate your own search results. You will then learn how an e-commerce website can be built using Elasticsearch. We will discuss various approaches in getting relevant content up the results, such as relevancy based on how well a query matched the text, time-based recent documents, geographically nearer items, and other frequently used approaches." , "publish_date":"2015-06-01"}}]}
Content-Type: application/json; charset=UTF-8
Content-Length: 1087

None

Process finished with exit code 0

原文地址:https://www.cnblogs.com/hzcya1995/p/13349510.html