如何在GitHub Actions中在Windows下发送POST请求?

huangapple go评论63阅读模式
英文:

How to send Post request in Github Actions under Windows?

问题

I need to notify Sentry about new build version when CI made it. I am using runs-on: windows-latest for builds and sending requests with a code below:

- name: Notify Sentry
  run: |
    $VERSION_NAME = "${{  github.ref_name }}"
    $REQUEST = "{\"version\": \"$VERSION_NAME\"}"
    echo $REQUEST
    curl -X POST -H "Content-Type:application/json" -d "$REQUEST" "https://sentry.io/api/hooks/release/builtin/...id.../...token.../"    

Request looks correct, but it always returns error Expecting property name enclosed in double quotes:

Run $VERSION_NAME = "${{  github.ref_name }}"
  $VERSION_NAME = "${{  github.ref_name }}"
  $REQUEST = "{\"version\": \"$VERSION_NAME\"}"
  echo $REQUEST
  curl -X POST -H "Content-Type:application/json" -d "$REQUEST" "https://sentry.io/api/hooks/release/builtin/...id.../...token.../"
  shell: C:\Program Files\PowerShell\pwsh.EXE -command "'.\{0\}'"

{"version": "v1.1.1"}
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   104  100    87  100    17    313     61 --:--:-- --:--:-- --:--:--   375
100   104  100    87  100    17    313     61 --:--:-- --:--:-- --:--:--   375
{"error":"Expecting property name enclosed in double quotes: line 1 column 2 (char 1)"}

In Postman it works well. How to fix this error?
英文:

I need to notify Sentry about new build version when CI made it. I am using runs-on: windows-latest for builds and sending requests with a code below:

- name: Notify Sentry
run: |
  $VERSION_NAME = "${{  github.ref_name }}"
  $REQUEST = "{`"version`": `"$VERSION_NAME`"}"
  echo $REQUEST
  curl -X POST -H "Content-Type:application/json" -d "$REQUEST" "https://sentry.io/api/hooks/release/builtin/...id.../...token.../"

Request looks correct, but it always returns error Expecting property name enclosed in double quotes:

Run $VERSION_NAME = "${{  github.ref_name }}"
  $VERSION_NAME = "${{  github.ref_name }}"
  $REQUEST = "{`"version`": `"$VERSION_NAME`"}"
  echo $REQUEST
  curl -X POST -H "Content-Type:application/json" -d "$REQUEST" "https://sentry.io/api/hooks/release/builtin/...id.../...token.../"
  shell: C:\Program Files\PowerShell\pwsh.EXE -command ". '{0}'"
  
{"version": "v1.1.1"}
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   104  100    87  100    17    313     61 --:--:-- --:--:-- --:--:--   375
100   104  100    87  100    17    313     61 --:--:-- --:--:-- --:--:--   375
{"error":"Expecting property name enclosed in double quotes: line 1 column 2 (char 1)"}

In Postman it works well. How to fix this error?

答案1

得分: 1

Use backslash instead of backtick to escape nested double quotes.

Change this:

$REQUEST = "{`"version`": `"$VERSION_NAME`"}"

to:

$REQUEST = "{\"version\": \"$VERSION_NAME\"}"

You may use single quotes instead of nested double ones and then use Replace function to bulk escape those like this:

$REQUEST = "{'version': '$VERSION_NAME'}".Replace("'",'\"')

See: https://stackoverflow.com/questions/60819537

英文:

Use backslash instead of backtick to escape nested double quotes.

Change this:

$REQUEST = "{`"version`": `"$VERSION_NAME`"}"

to:

$REQUEST = "{\"version\": \"$VERSION_NAME\"}"

You may use single quotes instead of nested double ones and then use Replace function to bulk escape those like this:

$REQUEST = "{'version': '$VERSION_NAME'}".Replace("'",'\"')

See: https://stackoverflow.com/questions/60819537

huangapple
  • 本文由 发表于 2023年4月13日 18:04:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定