简体   繁体   中英

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!
But when I called it inside .sh file it keeps returning HTTP/2 404


here is the Bash script:

#! /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.


Output difference:

  1. direct in terminal:
    • verbose log:
> 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!

  • output content:
{"code":false,"text":"No student exist with this NationalID"}
  1. within Bash:
    • verbose log:
> 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!

  • output content:
<!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?

With:

set -- "https://example.com/api/"${1}"-api.php"

You replace all the arguments list with only one argument (the URL). Then argument $2 becomes empty as it calls curl :

curl -S -v -d "$2" "$1"

You don't need to use set -- with bash as it has 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 --

#! /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:

#! /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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM