当我用Perl的REST::Client发送POST请求而不是用Perl的LWP::UserAgent或Python发送POST请求时,为什么会得到“405:Method Not Allowed”?

2024-05-16 00:19:46 发布

您现在位置:Python中文网/ 问答频道 /正文

当我用Perl的^{}发送POST请求时,我得到以下响应:

405: Method Not Allowed

但是,当我用Perl的^{}或Python的Requests向同一个URL发送POST请求时,我得到一个“success”响应。GET请求同时适用于REST::Client和Python。在

我该怎么解决这个问题?在


下面是使用REST::Client(返回HTTP 405)的代码:

^{pr2}$

下面是使用LWP::UserAgent(返回HTTP 200)的代码:

#!/use/bin/perl -w
use strict;
use LWP::UserAgent;
use Data::Dumper;
use JSON;

my $ua = LWP::UserAgent->new; 
my $req = HTTP::Request->new(POST => 'URL');

$req->header('Auth' => '04211b77df');
$req->header('Content-type' => 'application/json');
$req->header('Accept' => 'application/json');

my %body = (
    'sid' => '001',
    'pid' => 'c7b3d83',
    'file' => [
        {
            'name' => 'file1.txt',
            'location' => 'folderA'
        },
        {
            'name' => 'file2.txt',
            'location' => 'folderB'
        }
    ]
);
$req->content(encode_json(\%body));
$ua->request($req);

以下是Python代码(返回HTTP 200):

import requests, json, sys
headers = {
    'Auth': '04211b77df'
    'Accept': 'application/json'
    'Content-type': 'application/json'
}
data={
    'sid': '001',
    'pid': 'c7b3d83',
    'file': [
        {
            'name': 'file1.txt',
            'location': 'folderA'
        },
        {
            'name': 'file2.txt',
            'location': 'folderB'
        }
    ]
}

requests.request('POST', 'URL', data=data, headers=headers)

Tags: 代码nametxtjsonhttpurlapplicationuse
1条回答
网友
1楼 · 发布于 2024-05-16 00:19:46

您的REST::Client代码和LWP::UserAgent代码生成的请求几乎相同。*但是,REST::Client在幕后使用了LWP::UserAgent,它用$ua->simple_request()发送请求,而LWP::UserAgent代码使用$ua->request()。在

根据documentation

The difference from request() is that simple_request() will not try to handle redirects or authentication responses.

我猜是身份验证造成了这个问题。我还猜测,您使用的API不需要对GET请求进行身份验证,因为您说过这些请求是使用REST::Client的。在


要修复此问题,请在构造函数中将^{}选项设置为1

my $client = REST::Client->new(follow => 1);

或调用setFollow方法:

^{pr2}$

这将正确处理身份验证响应,但也将遵循重定向。不幸的是,您不能单独设置重定向和身份验证的行为,这是一个long-standing bug。在


*唯一的区别是REST::Client添加了一个Content-length头,我认为这无关紧要。在

发送之前转储REST::客户端请求:

$VAR1 = bless( { 
                 '_content' => '{"sid":"001","pid":"c7b3d83","file":[{"location":"folderA","name":"file1.txt"},{"location":"folderB","name":"file2.txt"}]}',
                 '_uri' => bless( do{\(my $o = 'http://www.example.com')}, 'URI::http' ),
                 '_headers' => bless( { 
                                        'auth' => '04211b77df',
                                        'content-type' => 'application/json',
                                        'accept' => 'application/json',
                                        'content-length' => 122,
                                        '::std_case' => { 
                                                          'auth' => 'Auth'
                                                        }
                                      }, 'HTTP::Headers' ),
                 '_method' => 'POST'
               }, 'HTTP::Request' );

发送前转储LWP::UserAgent请求:

$VAR1 = bless( { 
                 '_content' => '{"sid":"001","pid":"c7b3d83","file":[{"location":"folderA","name":"file1.txt"},{"location":"folderB","name":"file2.txt"}]}',
                 '_uri' => bless( do{\(my $o = 'http://www.example.com')}, 'URI::http' ),
                 '_headers' => bless( { 
                                        'auth' => '04211b77df',
                                        'content-type' => 'application/json',
                                        'accept' => 'application/json',
                                        '::std_case' => { 
                                                          'auth' => 'Auth'
                                                        }
                                      }, 'HTTP::Headers' ),
                 '_method' => 'POST'
               }, 'HTTP::Request' );

在实际发送请求时,还会有其他细微的差异,比如用户代理字符串,但我不认为它们与您的问题有关。在

相关问题 更多 >