0

I want to POST a json in bash script using curl but i am getting error that is dependent on the content..

I am getting error

Rebuilt URL to: "major":"1221",/
Illegal port number
Closing connection -1
curl: (3) Illegal port number
Note: Unnecessary use of -X or --request, POST is already inferred.
Rebuilt URL to: "minor":"32112",/
Illegal port number
Closing connection -1
curl: (3) Illegal port number
curl: (3) [globbing] unmatched close brace/bracket in column 48

This works

#!/bin/bash

param='[{"timestamp":"value","sourceId":"fe28edab963de6788"}]' 

echo $param
curl -d $param -H "Content-Type: application/json" -H "X-Security-AuthKey: 84C0712F-856D-4EC7-B136-FA39B5FFE995" -H "Type: DATA" -H "Device: RaspberryPI" -X POST "https://test.api.com/webhook" -v

but this does not work...

#!/bin/bash

param='[{"timestamp":"1554895106","sourceId":"fe28edab963de6788","uuid":"F7826DA6-4FA2-4E98-8024-BC5B71E0893E", "major":"1221", "minor":"32112", "rssi":"-63","distance":".26307557616382837295"}]'


echo $param
curl -d $param -H "Content-Type: application/json" -H "X-Security-AuthKey: 84C0712F-856D-4EC7-B136-FA39B5FFE995" -H "Type: DATA" -H "Device: RaspberryPI" -X POST "https://test.api.com/webhook" -v
LorenzOliveto
  • 7,796
  • 1
  • 20
  • 47
  • 1
    Add "double-quotes" around `$param` in the curl command. Your json is most certainly considered as multiple words, the first of which is associated with `-d` but the following of which are parsed as urls – Aaron Apr 10 '19 at 12:20

1 Answers1

2

Like @Aaron said, the shell is splitting up your value because it has spaces in it. By default, Bash splits "words" by spaces, tabs, and newlines. "weak quotes" (double quotes) will escape this word-splitting process while still allowing you to expand variables.

curl -d "$param" ...
vintnes
  • 2,014
  • 7
  • 16