如何在bash中执行CURL post命令?

时间:2015-08-06 06:18:17

标签: bash post curl

我正在尝试通过bash脚本执行curl post命令,我面临很多问题。

这是我的bash代码:

#!/bin/bash
url='http://slc760.us.oracle.com:10601/crmCommonApi/resources/latest'
Content='Content-Type:application/vnd.oracle.adf.batch+json'
user="sales_representative"
payload='{ "parts": [{"id": "part1","path": "/latest/__ORACO__Inventory_c","operation": "create","payload": {"__ORACO__Product_Id2_c":204,"__ORACO__Facing_c":"20","__ORACO__Product_Id1_c":204,"__ORACO__Product_c": "Ghirardelli White Choco","__ORACO__ShelfStock_c":"10","__ORACO__Location_c" : "Aisle 2","Organization_Id___ORACO__Account_Inventory":"300100051199355"}},{"id": "part1","path": "/latest/__ORACO__Inventory_c","operation": "create","payload": {"__ORACO__Product_Id2_c":204,"__ORACO__Facing_c":"3","__ORACO__Product_Id1_c":204,"__ORACO__Product_c": "Ghirardelli White Chocolcate","__ORACO__ShelfStock_c":"4","__ORACO__Location_c" : "Aisle 2","Organization_Id___ORACO__Account_Inventory":"300100051199355"}}]}'

curl -X POST -H "Content-Type:application/vnd.oracle.adf.batch+json" \
    --data $payload \
    $url \
    --user $user

运行时抛出一堆错误:

curl: (6) Could not resolve host: "parts"
curl: (3) [globbing] bad range specification in column 2
curl: (6) Could not resolve host: "part1","path"
curl: (6) Could not resolve host: "
curl: (6) Could not resolve host: "create","payload"
curl: (3) [globbing] unmatched brace in column 1
curl: (6) Could not resolve host: "Ghirardelli
curl: (6) Could not resolve host: White
curl: (6) Could not resolve host: Choco","__ORACO__ShelfStock_c"
curl: (7) Failed to connect to  port 80: Connection refused
curl: (6) Could not resolve host: "Aisle
curl: (3) [globbing] unmatched close brace/bracket in column 66
curl: (6) Could not resolve host: "part1","path"
curl: (6) Could not resolve host: "
curl: (6) Could not resolve host: "create","payload"
curl: (3) [globbing] unmatched brace in column 1
curl: (6) Could not resolve host: "Ghirardelli
curl: (6) Could not resolve host: White
curl: (6) Could not resolve host: Chocolcate","__ORACO__ShelfStock_c"
curl: (7) Failed to connect to  port 80: Connection refused
curl: (6) Could not resolve host: "Aisle
curl: (3) [globbing] unmatched close brace/bracket in column 66
org.codehaus.jackson.JsonParseException: Unexpected end-of-input: expected close marker for OBJECT (from [Source: weblogic.servlet.internal.ServletInputStreamImpl@10514b51; line: 1, column: 0])

当通过命令行分离执行时,命令似乎工作但是当我尝试在CURL中执行它时会抛出上述错误。我知道我相对于引号/语法缺少。

2 个答案:

答案 0 :(得分:1)

请尝试更改以下行:

--data $payload

--data '$payload'

答案 1 :(得分:1)

尝试在您的有效负载上加双引号:

curl -X POST -H "Content-Type:application/vnd.oracle.adf.batch+json" \
--data "$payload" \
$url \
--user $user

那应该有用。