how to insert json data in curl using visual studio code

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

how to insert json data in curl using visual studio code

问题

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

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

请求的结构定义如下:

type Product struct {
	ID          int     `json:"id"`
	Name        string  `json:"name"`
	Description string  `json:"description"`
	Price       float32 `json:"price"`
	SKU         string  `json:"sku"`
	CreatedOn   string  `json:"-"`
	UpdatedOn   string  `json:"-"`
	DeletedOn   string  `json:"-"`
}

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

英文:

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

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
*   Trying 127.0.0.1:9090...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to localhost (127.0.0.1) port 9090 (#0)
&gt; POST / HTTP/1.1
&gt; Host: localhost:9090
&gt; User-Agent: curl/7.83.1
&gt; Accept: */*
&gt; Content-Length: 35
&gt; Content-Type: application/x-www-form-urlencoded
&gt;
} [35 bytes data]
* Mark bundle as not supporting multiuse
&lt; HTTP/1.1 400 Bad Request
&lt; Content-Type: text/plain; charset=utf-8
&lt; X-Content-Type-Options: nosniff
&lt; Date: Tue, 03 Jan 2023 12:31:05 GMT
&lt; Content-Length: 25
&lt;
{ [25 bytes data]
100    60  100    25  100    35    989   1385 --:--:-- --:--:-- --:--:--  2857
* Connection #0 to host localhost left intact
curl: (3) unmatched close brace/bracket in URL position 4:
Tea}
   ^
parse error: Invalid numeric literal at line 1, column 7
PS C:\Golang&gt;

The structure definition for the request is

type Product struct {
	ID          int     `json:&quot;id&quot;`
	Name        string  `json:&quot;name&quot;`
	Description string  `json:&quot;description&quot;`
	Price       float32 `json:&quot;price&quot;`
	SKU         string  `json:&quot;sku&quot;`
	CreatedOn   string  `json:&quot;-&quot;`
	UpdatedOn   string  `json:&quot;-&quot;`
	DeletedOn   string  `json:&quot;-&quot;`
}

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

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

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

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

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

Try escaping quotation marks like below:

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:

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:

确定