[英]cURL return HTML error 404 instead of JSON when called within bash script requesting php API
when I use cURL directly in terminal like this curl -S -v -d codemeli=12345 https://example.com/api/score-api.php
everything OK!
但是當我在.sh
文件中調用它時,它一直返回HTTP/2 404
這是Bash
腳本:
#! /usr/bin/bash
if [ $# = 2 ]; then
set -- "https://example.com/api/"${1}"-api.php"
# t1=`date +%s`
curl -S -v -d "$2" "$1"
else
echo "you should specify only two arguments"
fi
第 3 行嘗試更新變量
$1
並在其前后添加通常的 URL 。
> POST /api/score-api.php HTTP/2
> Host: example.com
> Content-Length: 14
* We are completely uploaded and fine
< HTTP/2 200
輸出中刪除了各種行!
{"code":false,"text":"No student exist with this NationalID"}
> POST /api/score-api.php HTTP/2
> Host: example.com
> Content-Length: 0
< HTTP/2 404
* HTTP error before end of send, stop sending
第二行(
We are completely uploaded and fine
)不見了!
<!DOCTYPE html>
<html style="height:100%">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" >
<title> 404 Not Found
</title></head>
<body style="color: #444; margin:0;font: normal 14px/20px Arial, Helvetica, sans-serif; height:100%; background-color: #fff;">
<div style="height:auto; min-height:100%; "> <div style="text-align: center; width:800px; margin-left: -400px; position:absolute; top: 30%; left:50%;">
<h1 style="margin:0; font-size:150px; line-height:150px; font-weight:bold;">404</h1>
<h2 style="margin-top:20px;font-size: 30px;">Not Found
</h2>
<p>The resource requested could not be found on this server!</p>
</div></div></body></html>
POST上傳有問題還是我的bash腳本有問題?
和:
set -- "https://example.com/api/"${1}"-api.php"
您只需用一個參數(URL)替換所有 arguments 列表。 然后參數$2
在調用curl
時變為空:
curl -S -v -d "$2" "$1"
您不需要使用set --
bash,因為它有 arrays。 即使您使用它也不需要它,因為它根本不處理數組數據。 無論如何,這是您在使用set --
#! /usr/bin/bash
if [ $# = 2 ]; then
set -- "https://example.com/api/"${1}"-api.php" "$2"
# t1=`date +%s`
curl -S -v -d "$2" "$1"
else
echo "you should specify only two arguments"
fi
這是不使用set --
並使 arguments 不可變,這是一種更好的編碼實踐:
#! /usr/bin/bash
if [ $# = 2 ]; then
url="https://example.com/api/$1-api.php"
# t1=`date +%s`
data="$2"
curl -S -v -d "$data" "$url"
else
echo "you should specify only two arguments"
fi
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.