Perl CGI脚本响应代码

时间:2015-08-01 07:27:58

标签: android perl cgi

我有一个Perl脚本,它接受HTTP GET请求并返回JSON内容。但是,当我使用Volley从Android应用程序调用脚本时,我收到以下错误:

@IBOutlet weak var TextView: UITextView!
var TextFromFile = String()

override func viewDidLoad()
{
    super.viewDidLoad()
    TextView.text = TextFromFile
    let path = NSBundle.mainBundle().pathForResource("TextFile", ofType: "txt")

    if let content = String(contentsOfFile:path!, encoding: NSUTF8StringEncoding, error: nil) {

        let lines : [String] = content.componentsSeparatedByString("/n")

        TextView.text = content
    }

如果我使用curl检查脚本,我会收到此回复:

E/Volley﹕ [1178] BasicNetwork.performRequest: Unexpected response code 500 for http://

以下是perl代码:

* Hostname was NOT found in DNS cache
*   Trying 95.142.152.194...
* Connected to pro.url.co.uk (127.0.0.1) port 80 (#0)
> GET /cgi-bin/api.pl HTTP/1.1
> User-Agent: curl/7.37.1
> Host: pro.url.co.uk
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Sat, 01 Aug 2015 07:18:42 GMT
* Server Apache is not blacklisted
< Server: Apache
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: application/json; charset=utf-8
< Set-Cookie: DYNSRV=lin218; path=/
< 
{"error":"2000","description":"an action header is required"}

我不是世界上最有能力的perl开发者,所以如果有人有任何提示,我们将非常感激。

1 个答案:

答案 0 :(得分:0)

过去,我必须在标题后面加上几个空行,以防止apache生成500错误。

所以试试这段代码:

my $cgi = CGI->new;
my $json = JSON->new->utf8;
my $header_type = "application/json";
my $header_status = "200";
my $output = $json->encode({
  "error" => "2000",
  "description" => "an action header is required",
});
print $cgi->header(
  -type => $header_type,
  -charset => "utf-8",
  -status => $header_status
),
 "\n\n",
 $output,
 "\n";