0

I am trying to execute curl command from shell script. This command works properly from cli. However, I need to execute it in shell script. The curl command is expected to delete records on solr depending upon the query.

curl -v  -o /dev/null  -w %{http_code}    -H Content-Type:text/xml -X POST   http://localhost:8983/solr/content/update?commit=true -d  <delete><query>playlist_id:["" TO *]</query></delete> 

Note: Unnecessary use of -X or --request, POST is already inferred.
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8983 (#0)
> POST /solr/content/update?commit=true HTTP/1.1
> Host: localhost:8983
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Type:text/xml
> Content-Length: 30
> 
} [30 bytes data]
* upload completely sent off: 30 out of 30 bytes
< HTTP/1.1 400 Bad Request
< Content-Type: application/xml; charset=UTF-8
< Content-Length: 488
< 
{ [488 bytes data]
100   518  100   488  100    30  63649   3912 --:--:-- --:--:-- --:--:-- 69714
* Connection #0 to host localhost left intact
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: TO/
* Could not resolve host: TO
* Closing connection 1
curl: (6) Could not resolve host: TO
curl: (3) [globbing] unmatched close brace/bracket in column 2
400000
status: '400000'
Critical error happened during solr data delete: Verify the state manually
Quitting now

I need to retrieve http status code to verify the status. Following shell script code is executed.

curl_command_tmp="http://localhost:8983/solr/content/update?commit=true -d <delete><query>playlist_id:["" TO *]</query></delete>"
    curl_command_tmp="curl -v  -o /dev/null  -w %{http_code}    -H Content-Type:text/xml -X POST   $curl_command_tmp"
            echo "Curl delete command $curl_command_tmp"
           status=`$curl_command_tmp`
            echo $status
            echo "status: '$status'"
           if [ "$status" != "200" ]; then
                     echo "Critical error happened during solr data delete: Verify the state manually"
                     echo "Quitting now"
                     exit 0
           fi

I have tried different options of curl (e.g. -G , --data-urlencode). but they do not work. I guess if i can pass complete body of request without getting it truncated , then it will work. But it gets truncated before "TO" in the solr http request body. I have gone through various forums but it is not resolved yet. Need your help to resolve it

acorntech
  • 53
  • 8
  • The double-quotes here `["" TO *]` are confusing the script shell. It thinks the first `"` matches the `"` at the start of the command, and the second `""` begins a new string matched by the `"` at the end of the command. The result is that that `""` disappears. Try rewriiting that expression by using a backslash to protect each of those double-quotes from being treated as string delimiters, like `[\"\" TO *]`. – ottomeister Jan 27 '18 at 23:23

1 Answers1

0

I tried the suggestion in the comment, but it did not work. Instead i put the data (<delete><query>playlist_id:["" TO *]</query></delete>) in file and used it like this:

-d @FILE_NAME

This worked without any flaw. My guess is if the data has special characters which curl can not handle on cli then use file instead.

Note:- I did not put any extra quotes or double quotes in FILE. Just put the data which actually needs to be sent to.

acorntech
  • 53
  • 8