일부 고객을 직접 방문하지 않고도 고객 지원 소프트웨어에 삽입하도록 기본 코드를 설정하려고합니다. 코드에 변수를 추가 할 때 문제가있는 것 같습니다.변수를 사용하여 cURL POST 요청
이 코드는 작동합니다
#!/bin/sh
curl https://yoursite.desk.com/api/v2/customers \
-u username:password \
-X POST \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d '{
"first_name":"John",
"last_name":"Doe",
"phone_numbers":
[
{
"type":"Other",
"value":"5555555555"
}
],
"emails":
[
{
"type": "other",
"value":"[email protected]
}
],
"custom_fields":
{
"field_a":"12345"
}
}'
내가왔다으로이 코드는 항상 가끔 오류 코드가 표시됩니다 코드를 조정, 그것은 가치가 무엇인지에 대한 오류 '잘못된 JSON'
#!/bin/sh
first=John
last=Doe
phone=5555555555
phone_type=other
[email protected]
email_type=other
id=12345
curl https://yoursite.desk.com/api/v2/customers \
-u username:password \
-X POST \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d '{
"first_name":'"$first"',
"last_name":'"$last"',
"phone_numbers":
[
{
"type":'"$phone_type"',
"value":'"$phone"'
}
],
"emails":
[
{
"type":'"$email_type"',
"value":'"$email"'
}
],
"custom_fields":
{
"field_a":'"$id"'
}
}'
을 반환 "이메일": "값": (유효하지 않음) 및 "전화 번호": "가치":(무효)
내가 강하게 JSON을 생성하는'jq' 같은 것을 사용하는 것이 좋습니다. 그러면 JSON으로 올바르게 인코딩됩니다. 'jq ... | 컬 ... -d @ - ...'. – chepner