英文:
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> 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>
The structure definition for the request is
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:"-"`
}
I went through some of the older question they were suggesting to send the data as
d '{"id": 1", "name": "tea","description": "Nice Tea"}' 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 '{\"id\": 1, \"name\": \"tea\",\"description\": \"Nice Tea\"}' 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 '@dummy.json' http://localhost:9090/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论