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

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

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:

  1. - name: Notify Sentry
  2. run: |
  3. $VERSION_NAME = "${{ github.ref_name }}"
  4. $REQUEST = "{\"version\": \"$VERSION_NAME\"}"
  5. echo $REQUEST
  6. 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:

  1. Run $VERSION_NAME = "${{ github.ref_name }}"
  2. $VERSION_NAME = "${{ github.ref_name }}"
  3. $REQUEST = "{\"version\": \"$VERSION_NAME\"}"
  4. echo $REQUEST
  5. curl -X POST -H "Content-Type:application/json" -d "$REQUEST" "https://sentry.io/api/hooks/release/builtin/...id.../...token.../"
  6. shell: C:\Program Files\PowerShell\pwsh.EXE -command "'.\{0\}'"
  7. {"version": "v1.1.1"}
  8. % Total % Received % Xferd Average Speed Time Time Time Current
  9. Dload Upload Total Spent Left Speed
  10. 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
  11. 100 104 100 87 100 17 313 61 --:--:-- --:--:-- --:--:-- 375
  12. 100 104 100 87 100 17 313 61 --:--:-- --:--:-- --:--:-- 375
  13. {"error":"Expecting property name enclosed in double quotes: line 1 column 2 (char 1)"}
  14. 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:

  1. - name: Notify Sentry
  2. run: |
  3. $VERSION_NAME = "${{ github.ref_name }}"
  4. $REQUEST = "{`"version`": `"$VERSION_NAME`"}"
  5. echo $REQUEST
  6. 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:

  1. Run $VERSION_NAME = "${{ github.ref_name }}"
  2. $VERSION_NAME = "${{ github.ref_name }}"
  3. $REQUEST = "{`"version`": `"$VERSION_NAME`"}"
  4. echo $REQUEST
  5. curl -X POST -H "Content-Type:application/json" -d "$REQUEST" "https://sentry.io/api/hooks/release/builtin/...id.../...token.../"
  6. shell: C:\Program Files\PowerShell\pwsh.EXE -command ". '{0}'"
  7. {"version": "v1.1.1"}
  8. % Total % Received % Xferd Average Speed Time Time Time Current
  9. Dload Upload Total Spent Left Speed
  10. 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
  11. 100 104 100 87 100 17 313 61 --:--:-- --:--:-- --:--:-- 375
  12. 100 104 100 87 100 17 313 61 --:--:-- --:--:-- --:--:-- 375
  13. {"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:

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

to:

  1. $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:

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

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

英文:

Use backslash instead of backtick to escape nested double quotes.

Change this:

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

to:

  1. $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:

  1. $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:

确定