1

I am tearing my hair out on this. I am attempting the following:

curl -S https/api.bitbucket.org/2.0/repositories/etc/etc/pullrequests/comments \
-u username:pw
--request POST --header 'Content-Type: application/json' \
-d '{"content": {"text": "'"$logMsg"'"}}'

This, even with the -S throws back an ambiguous bad request. The $logMsg has a large number of special characters and newlines, an example of what it looks like may be:

"\n\n***HelloThere**  <->:+/"

Note that whatever the variable is should be wrapped in double quotes. I have no idea what is going on with bash with how it's interpreting that variable because copying and pasting the string literal into postman works perfectly. Example:

--data-raw '{"content": { "raw": "\n\n***HelloThere**<->:+/"}}'

I have a feeling there is some string interpolation going on (IE turning \n into an actual newline) but I am unsure how to resolve.

JayDee
  • 45
  • 8
  • Try adding -s to the curl command. The man page says -S will only work in conjunction with the silent option. – Cole Tierney Mar 28 '23 at 14:09
  • you meant `https://...` didn't you? Good luck. – shellter Mar 28 '23 at 14:39
  • 1
    Your `$logMsg` isn't valid JSON. Strings need to be encoded as JSON before they can be inserted into JSON documents; this is what `jq` is for. There are a lot of examples on the Internet that just _assume_ that strings won't include any characters that need to be escaped, but those examples are bad/wrong/evil, as you just discovered. :) – Charles Duffy Mar 28 '23 at 14:39
  • @CharlesDuffy turns out this was the issue, after hours of plugging away there were upstream echo related issues cause certain characters to not escape properly. – JayDee Mar 29 '23 at 18:17

1 Answers1

5

Don't try to generate the JSON by hand. Use jq.

jq --arg lm "$logMsg" '{content: {text: $lm}}' | curl ... -d @-
chepner
  • 497,756
  • 71
  • 530
  • 681
  • This didn't solve the issue but using jq to test the validity of the json gave a big clue to that the $logMsg output was causing json validity issue. – JayDee Mar 29 '23 at 18:19