how to insert json data in curl using visual studio code

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

how to insert json data in curl using visual studio code

问题

我正在尝试在Visual Studio Code中使用curl插入数据。

  1. PS C:\Golang> curl -v http://localhost:9090/ -d '{"id": 1, "name": "tea","description": "Nice Tea"}' |jq
  2. * Trying 127.0.0.1:9090...
  3. % Total % Received % Xferd Average Speed Time Time Time Current
  4. Dload Upload Total Spent Left Speed
  5. 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to localhost (127.0.0.1) port 9090 (#0)
  6. > POST / HTTP/1.1
  7. > Host: localhost:9090
  8. > User-Agent: curl/7.83.1
  9. > Accept: */*
  10. > Content-Length: 35
  11. > Content-Type: application/x-www-form-urlencoded
  12. >
  13. } [35 bytes data]
  14. * Mark bundle as not supporting multiuse
  15. < HTTP/1.1 400 Bad Request
  16. < Content-Type: text/plain; charset=utf-8
  17. < X-Content-Type-Options: nosniff
  18. < Date: Tue, 03 Jan 2023 12:31:05 GMT
  19. < Content-Length: 25
  20. <
  21. { [25 bytes data]
  22. 100 60 100 25 100 35 989 1385 --:--:-- --:--:-- --:--:-- 2857
  23. * Connection #0 to host localhost left intact
  24. curl: (3) unmatched close brace/bracket in URL position 4:
  25. Tea}
  26. ^
  27. parse error: Invalid numeric literal at line 1, column 7
  28. PS C:\Golang>

请求的结构定义如下:

  1. type Product struct {
  2. ID int `json:"id"`
  3. Name string `json:"name"`
  4. Description string `json:"description"`
  5. Price float32 `json:"price"`
  6. SKU string `json:"sku"`
  7. CreatedOn string `json:"-"`
  8. UpdatedOn string `json:"-"`
  9. DeletedOn string `json:"-"`
  10. }

我查看了一些旧的问题,他们建议将数据发送为d '{"id": 1, "name": "tea","description": "Nice Tea"}',但仍然失败了。请帮助我,提前感谢。

英文:

I am trying to insert the data using curl in Visual studio code.

  1. PS C:\Golang&gt; curl -v http://localhost:9090/ -d &#39;{&quot;id&quot;: 1, &quot;name&quot;: &quot;tea&quot;,&quot;description&quot;: &quot;Nice Tea&quot;}&#39; |jq
  2. * Trying 127.0.0.1:9090...
  3. % Total % Received % Xferd Average Speed Time Time Time Current
  4. Dload Upload Total Spent Left Speed
  5. 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to localhost (127.0.0.1) port 9090 (#0)
  6. &gt; POST / HTTP/1.1
  7. &gt; Host: localhost:9090
  8. &gt; User-Agent: curl/7.83.1
  9. &gt; Accept: */*
  10. &gt; Content-Length: 35
  11. &gt; Content-Type: application/x-www-form-urlencoded
  12. &gt;
  13. } [35 bytes data]
  14. * Mark bundle as not supporting multiuse
  15. &lt; HTTP/1.1 400 Bad Request
  16. &lt; Content-Type: text/plain; charset=utf-8
  17. &lt; X-Content-Type-Options: nosniff
  18. &lt; Date: Tue, 03 Jan 2023 12:31:05 GMT
  19. &lt; Content-Length: 25
  20. &lt;
  21. { [25 bytes data]
  22. 100 60 100 25 100 35 989 1385 --:--:-- --:--:-- --:--:-- 2857
  23. * Connection #0 to host localhost left intact
  24. curl: (3) unmatched close brace/bracket in URL position 4:
  25. Tea}
  26. ^
  27. parse error: Invalid numeric literal at line 1, column 7
  28. PS C:\Golang&gt;

The structure definition for the request is

  1. type Product struct {
  2. ID int `json:&quot;id&quot;`
  3. Name string `json:&quot;name&quot;`
  4. Description string `json:&quot;description&quot;`
  5. Price float32 `json:&quot;price&quot;`
  6. SKU string `json:&quot;sku&quot;`
  7. CreatedOn string `json:&quot;-&quot;`
  8. UpdatedOn string `json:&quot;-&quot;`
  9. DeletedOn string `json:&quot;-&quot;`
  10. }

I went through some of the older question they were suggesting to send the data as
d '{&quot;id&quot;: 1", &quot;name&quot;: &quot;tea&quot;,&quot;description&quot;: &quot;Nice Tea&quot;}' but still it failed.
Please help me and thanks in advance.

答案1

得分: 2

尝试像下面这样转义引号:

  1. curl -v --data '{\"id\": 1, \"name\": \"tea\",\"description\": \"Nice Tea\"}' http://localhost:9090/

或者如果你有复杂的 JSON 数据并且想避免转义,你可以将 JSON 请求体作为文件传递:

  1. curl -v -d '@dummy.json' http://localhost:9090/
英文:

Try escaping quotation marks like below:

  1. curl -v --data &#39;{\&quot;id\&quot;: 1, \&quot;name\&quot;: \&quot;tea\&quot;,\&quot;description\&quot;: \&quot;Nice Tea\&quot;}&#39; http://localhost:9090/

Or if you have complex json data and you want to avoid escaping, you can pass in the json request body as a file:

  1. curl -v -d &#39;@dummy.json&#39; http://localhost:9090/

huangapple
  • 本文由 发表于 2023年1月3日 20:37:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/74993483.html
匿名

发表评论

匿名网友

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

确定