【问题标题】:curl command line equivalent to this perl codecurl 命令行相当于这个 perl 代码
【发布时间】:2015-08-06 05:07:49
【问题描述】:

我想为与此 Perl 代码等效的 POST 请求编写 curl 命令:

use strict;
use warnings;
use LWP::UserAgent;
my $base = 'http://www.uniprot.org/mapping/';
my $params = {
  from => 'ACC',
  to => 'P_REFSEQ_AC',
  format => 'tab',
  query => 'P13368'
};
my $agent = LWP::UserAgent->new();
push @{$agent->requests_redirectable}, 'POST';
my $response = $agent->post($base, $params);
$response->is_success ?
  print $response->content :
  die 'Failed, got ' . $response->status_line .
    ' for ' . $response->request->uri . "\n";

我尝试过(以及许多其他变体):

curl -X POST -H "Expect:" --form "from=ACC;to=P_REFSEQ_AC;format=tab; query=P13368" http://www.uniprot.org/mapping/ -o out.tab

Perl 代码检索预期结果,但 curl 命令行没有。它从“http://www.uniprot.org/mapping/”检索网页,但不发出 POST 请求。

我在响应标头中查找错误,但没有发现任何可疑之处。

> POST http://www.uniprot.org/mapping/ HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: www.uniprot.org
> Accept: */*
> Proxy-Connection: Keep-Alive
> Content-Length: 178
> Content-Type: multipart/form-data; boundary=----------------------------164471d8347f
> 
} [data not shown]
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Server: Apache-Coyote/1.1
< Vary: User-Agent
< Vary: Accept-Encoding
< X-Hosted-By: European Bioinformatics Institute
< Content-Type: text/html;charset=UTF-8
< Date: Wed, 05 Aug 2015 20:32:00 GMT
< X-UniProt-Release: 2015_08
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Headers: origin, x-requested-with, content-type
< X-Cache: MISS from localhost
< X-Cache-Lookup: MISS from localhost:3128
< Via: 1.0 localhost (squid/3.1.20)
< Connection: close
<

我花了将近三天的时间在网上寻找解决方案,但没有什么对我有用。

【问题讨论】:

    标签: perl curl


    【解决方案1】:

    看起来服务器期望数据为application/x-www-form-urlencoded,而不是multipart/form-data,就像您使用--form 参数所做的那样。以下应该有效:

    curl -v -L --data \
      "from=ACC&to=P_REFSEQ_AC&format=tab&query=P13368" \
      http://www.uniprot.org/mapping/ -o out.tab
    

    使用--data,您将获得预期的内容类型标头,但您必须自己进行编码。使用-L curl 会遵循此处需要的重定向来获取结果数据。

    不需要-X POST 选项,因为 POST 是发送数据时的默认方法。并且-H "Expect:" 也不需要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-24
      • 1970-01-01
      • 1970-01-01
      • 2017-08-08
      • 2014-02-02
      • 2011-06-03
      • 2014-08-28
      相关资源
      最近更新 更多