[英]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! when I use cURL directly in terminal like this
curl -S -v -d codemeli=12345 https://example.com/api/score-api.php
everything OK!
But when I called it inside .sh
file it keeps returning HTTP/2 404
但是当我在
.sh
文件中调用它时,它一直返回HTTP/2 404
here is the Bash
script:这是
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
Line 3 trying to update variable
$1
and add usual URL before & after it.第 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
various line deleted in output!
输出中删除了各种行!
{"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
Second line (
We are completely uploaded and fine
) gone!第二行(
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>
Is there problem of POST upload or my bash script wrong? POST上传有问题还是我的bash脚本有问题?
With:和:
set -- "https://example.com/api/"${1}"-api.php"
You replace all the arguments list with only one argument (the URL).您只需用一个参数(URL)替换所有 arguments 列表。 Then argument
$2
becomes empty as it calls curl
:然后参数
$2
在调用curl
时变为空:
curl -S -v -d "$2" "$1"
You don't need to use set --
with bash as it has arrays.您不需要使用
set --
bash,因为它有 arrays。 It is not needed even for your usage since it does not deal with array data at all.即使您使用它也不需要它,因为它根本不处理数组数据。 Anyway, here is your code fixed while still using
set --
无论如何,这是您在使用
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
Here is equivalent code without using set --
and making arguments immutable, witch is a better coding practice:这是不使用
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.