curl perl Python post 发送自定义请求头

Star A File

POST https://cloud.seafile.com/api2/starredfiles/

Request parameters

repo_id (post)
p (post)


curl -v -d "repo_id=0219ecf6-0602-4aa7-b9f2-e678255945e5&p=/elk网址.txt" -H 'Authorization: Token 0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7' -H 'Accept: application/json; charset=utf-8; indent=4' http://192.168.137.1:8000/api2/starredfiles/


perl 版本:

##给文件加星标
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 $token_url= 'http://127.0.0.1:8000/api2/auth-token/';
                
  
   my $res = $ua->post($token_url,
                {
                'username'=>'015208@zjtlcb.com',
                'password'=>'1234567'
                });
   print $res->content();
   print "
";
      my $r= $res->content();
   my $r=encode_utf8($r);
   my $hash = decode_json($r);
   my $token =$hash->{"token"};
   print "$r is $r
";
   
   my $url="http://192.168.137.1:8000/api2/starredfiles/";
   my $res = $ua->post($url,
                {
                'repo_id'=>'0219ecf6-0602-4aa7-b9f2-e678255945e5',
				 'p'=>'/elk网址.txt'
                },
				'accept'=> "application/json; indent=4",  
    'content-type'=> "application/x-www-form-urlencoded",  
    'Authorization'=> "Token  $token" 
				);
   print $res->content();
   print "
";
   print $res->status_line();


python 代码;
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import urllib
import cookielib
import json
import httplib
import requests

def gettoken():
     data = {'username': '99999@zjtlcb.com', 'password': '1234567'}
     post_data = urllib.urlencode(data)  # 将post消息化成可以让服务器编码的方式
     cj = cookielib.CookieJar()  # 获取cookiejar实例
     opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
     # 自己设置User-Agent(可用于伪造获取,防止某些网站防ip注入)
     headers = {}
     website = "http://127.0.0.1:8000/api2/auth-token/"
     req = urllib2.Request(website, post_data, headers)
     content = opener.open(req)
     s = content.read()  # linux下没有gbk编码,只有utf-8编码
     print s
     print type(s)
     text = json.loads(s)
     print type(text)
     print text['token']
     token = text['token']
     return token


def add_starred_files():
    token = gettoken()
    token = 'Token' + ' '+ token
    print token
    data = {'repo_id': '0219ecf6-0602-4aa7-b9f2-e678255945e5', 'p': '/elk网址.txt'}
    post_data = urllib.urlencode(data)  # 将post消息化成可以让服务器编码的方式
    print post_data
    cj = cookielib.CookieJar()  # 获取cookiejar实例
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    # 自己设置User-Agent(可用于伪造获取,防止某些网站防ip注入)
    headers = {"Authorization": token, "Accept": "application/json; indent=4", "content-type": "application/x-www-form-urlencoded"}
    website = 'http://127.0.0.1:8000/api2/starredfiles/'
    req = urllib2.Request(website, post_data, headers)
    content = opener.open(req)
    s = content.read()  # linux下没有gbk编码,只有utf-8编码
    print s
add_starred_files()

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