登录
首页 >  Golang >  Go问答

使用Visual Studio代码向curl命令中添加JSON数据

来源:stackoverflow

时间:2024-03-07 16:36:26 446浏览 收藏

偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《使用Visual Studio代码向curl命令中添加JSON数据》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!

问题内容

我正在尝试在 visual studio 代码中使用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"}' 但仍然失败。 请帮助我并提前致谢。


正确答案


尝试转义引号,如下所示:

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

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

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

终于介绍完啦!小伙伴们,这篇关于《使用Visual Studio代码向curl命令中添加JSON数据》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>