kam*_*aci 2606 rest json curl spring-mvc http-headers
我使用Ubuntu并在其上安装了Curl.我想用Curl测试我的Spring REST应用程序.我在Java端编写了我的POST代码.但是,我想用Curl测试它.我正在尝试发布JSON数据.示例数据如下:
{"value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true}
Run Code Online (Sandbox Code Playgroud)
我用这个命令:
curl -i \
-H "Accept: application/json" \
-H "X-HTTP-Method-Override: PUT" \
-X POST -d "value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true \
http://localhost:8080/xx/xxx/xxxx
Run Code Online (Sandbox Code Playgroud)
它返回此错误:
HTTP/1.1 415 Unsupported Media Type
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 1051
Date: Wed, 24 Aug 2011 08:50:17 GMT
Run Code Online (Sandbox Code Playgroud)
错误描述如下:
服务器拒绝此请求,因为请求实体的格式不受所请求方法()的请求资源支持.
Tomcat日志:"POST/ui/webapp/conf/clear HTTP/1.1"415 1051
关于Curl命令的正确格式的任何想法?
编辑:
这是我的Java端PUT代码(我测试过GET和DELETE,它们有效)
@RequestMapping(method = RequestMethod.PUT)
public Configuration updateConfiguration(HttpServletResponse response, @RequestBody Configuration configuration) { //consider @Valid tag
configuration.setName("PUT worked");
//todo If error occurs response.sendError(HttpServletResponse.SC_NOT_FOUND);
return configuration;
}
Run Code Online (Sandbox Code Playgroud)
Sea*_*oyd 4040
您需要将content-type设置为application/json.但是-d
发送Content-Type application/x-www-form-urlencoded
,这是Spring方面不接受的.
-H "Content-Type: application/json"
Run Code Online (Sandbox Code Playgroud)
完整示例:
curl --header "Content-Type: application/json" \
--request POST \
--data '{"username":"xyz","password":"xyz"}' \
http://localhost:3000/api/login
Run Code Online (Sandbox Code Playgroud)
(-H
是为短--header
,-d
为--data
)
请注意,如果您使用,则该选项-request POST
是可选的-d
,因为该-d
标志表示POST请求.
在Windows上,情况略有不同.查看评论主题.
小智 516
尝试将您的数据放入文件中,body.json
然后使用
curl -H "Content-Type: application/json" --data @body.json http://localhost:8080/ui/webapp/conf
Run Code Online (Sandbox Code Playgroud)
mo-*_*eph 94
你可能会发现有用的东西:https: //github.com/micha/resty
它是CURL的包装器,简化了命令行REST请求.您将其指向您的API端点,它为您提供PUT和POST命令.(从主页改编的例子)
$ resty http://127.0.0.1:8080/data #Sets up resty to point at your endpoing
$ GET /blogs.json #Gets http://127.0.0.1:8080/data/blogs.json
#Put JSON
$ PUT /blogs/2.json '{"id" : 2, "title" : "updated post", "body" : "This is the new."}'
# POST JSON from a file
$ POST /blogs/5.json < /tmp/blog.json
Run Code Online (Sandbox Code Playgroud)
此外,通常仍需要添加内容类型标头.但是,您可以执行一次设置默认的每站点每个方法添加配置文件:设置默认RESTY选项
小智 84
对于Windows,对该-d
值使用单引号对我不起作用,但在更改为双引号后它确实有效.此外,我需要在大括号内转义双引号.
也就是说,以下不起作用:
curl -i -X POST -H "Content-Type: application/json" -d '{"key":"val"}' http://localhost:8080/appname/path
Run Code Online (Sandbox Code Playgroud)
但以下工作:
curl -i -X POST -H "Content-Type: application/json" -d "{\"key\":\"val\"}" http://localhost:8080/appname/path
Run Code Online (Sandbox Code Playgroud)
小智 82
它对我有用:
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"id":100}' http://localhost/api/postJsonReader.do
Run Code Online (Sandbox Code Playgroud)
很高兴映射到Spring控制器:
@RequestMapping(value = "/postJsonReader", method = RequestMethod.POST)
public @ResponseBody String processPostJsonData(@RequestBody IdOnly idOnly) throws Exception {
logger.debug("JsonReaderController hit! Reading JSON data!"+idOnly.getId());
return "JSON Received";
}
Run Code Online (Sandbox Code Playgroud)
IdOnly
是一个带有id属性的简单POJO.
小智 52
例如,创建一个JSON文件params.json,并将此内容添加到它:
[
{
"environment": "Devel",
"description": "Machine for test, please do not delete!"
}
]
Run Code Online (Sandbox Code Playgroud)
然后运行此命令:
curl -v -H "Content-Type: application/json" -X POST --data @params.json -u your_username:your_password http://localhost:8000/env/add_server
Run Code Online (Sandbox Code Playgroud)
jlr*_*jlr 40
jo value=30 type="Tip 3" targetModule="Target 3" configurationGroup=null name="Configuration Deneme 3" description=null identity="Configuration Deneme 3" | \
curl --json @- \
-X POST \
http://localhost:8080/xx/xxx/xxxx | \
jq
Run Code Online (Sandbox Code Playgroud)
这将覆盖缺少的必要标头:无需显式定义Content-Type
和Accept
标头。
2022 年 3 月上旬,curl
发布了新的命令行参数 --json
版本 7.82.0。这允许通过 JSON 发送快捷方式,并且无需定义Content-Type
您缺少的内容和Accept
标头,因为这些是自动假定的,从而降低了错误风险:
curl --json '{"tool": "curl"}' https://example.com/
Run Code Online (Sandbox Code Playgroud)
但是等等...还有更多。不要将 json 参数定义为命令行的字符串curl
,而是使用漂亮的jo
CLI 工具将 JSON 定义为一系列键值对,并通过curl 通过管道传输输出。仅用于jo
定义 JSON,其工作方式如下:
> jo -p value=30 type="Tip 3" targetModule="Target 3" configurationGroup=null name="Configuration Deneme 3" description=null identity="Configuration Deneme 3"
version=0 systemId=3 active=true
{
"value": 30,
"type": "Tip 3",
"targetModule": "Target 3",
"configurationGroup": null,
"name": "Configuration Deneme 3",
"description": null,
"identity": "Configuration Deneme 3",
"version": 0,
"systemId": 3,
"active": true
}
Run Code Online (Sandbox Code Playgroud)
现在让我们使用类似的curl
命令来展示这一点,但没有额外的标头并使用jo
+jq
来获得良好的输出:
jo value=30 type="Tip 3" targetModule="Target 3" configurationGroup=null name="Configuration Deneme 3" description=null identity="Configuration Deneme 3" | \
curl --json @- \
-X POST \
http://localhost:8080/xx/xxx/xxxx | \
jq
Run Code Online (Sandbox Code Playgroud)
使用免费的模拟 API进行演示:
> jo title="Blog Post" body="lorem ipsum" userId=33 | \
curl --json @- \
-X POST \
https://jsonplaceholder.typicode.com/posts | \
jq
Run Code Online (Sandbox Code Playgroud)
由于以下原因,输出具有漂亮的格式jq
:
{
"title": "Blog Post",
"body": "lorem ipsum",
"userId": 33,
"id": 101
}
Run Code Online (Sandbox Code Playgroud)
fel*_*gnu 36
这对我很有用.
curl -X POST --data @json_out.txt http://localhost:8080/
Run Code Online (Sandbox Code Playgroud)
哪里,
-X
表示http动词.
--data
表示您要发送的数据.
Ste*_*ler 34
我遇到了同样的问题.我可以通过指定来解决它
-H "Content-Type: application/json; charset=UTF-8"
Run Code Online (Sandbox Code Playgroud)
小智 28
使用CURL Windows,试试这个:
curl -X POST -H "Content-Type:application/json" -d "{\"firstName\": \"blablabla\",\"lastName\": \"dummy\",\"id\": \"123456\"}" http-host/_ah/api/employeeendpoint/v1/employee
Run Code Online (Sandbox Code Playgroud)
tos*_*osh 20
HTTPie是推荐的替代品,curl
因为你可以做到
$ http POST http://example.com/some/endpoint name=value name1=value1
Run Code Online (Sandbox Code Playgroud)
默认情况下它会说JSON,并且会处理为您设置必要的标头以及将数据编码为有效的JSON.还有:
Some-Header:value
Run Code Online (Sandbox Code Playgroud)
对于标题,和
name==value
Run Code Online (Sandbox Code Playgroud)
用于查询字符串参数.如果您有大量数据,您也可以从文件中读取它,如果它是JSON编码的:
field=@file.txt
Run Code Online (Sandbox Code Playgroud)
Ind*_*our 17
我知道,这个问题已经有很多答案了,但我想分享一下我遇到的问题:
curl -X POST http://your-server-end-point -H "Content-Type: application/json" -d @path-of-your-json-file.json
看,我做的一切都对,只有一件事 - 在 JSON 文件路径之前我错过了“@”。
我在互联网上找到了一份相关的文档 - https://gist.github.com/subfuzion/08c5d85437d5d4f00e58
希望能帮到少数人。谢谢
dav*_*pcj 16
This worked well for me, additionally using BASIC authentication:
curl -v --proxy '' --basic -u Administrator:password -X POST -H "Content-Type: application/json"
--data-binary '{"value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true}'
http://httpbin.org/post
Run Code Online (Sandbox Code Playgroud)
Of course, you should never use BASIC authentication without SSL and a checked certificate.
我今天再次遇到这个问题,使用Cygwin的cURL 7.49.1 for Windows ...当使用--data
或--data-binary
使用JSON参数时,cURL感到困惑,并将{}
JSON中的解释为URL模板.添加一个-g
参数来关闭cURL globbing修复了那个问题.
另请参阅将带括号的URL传递给curl.
rek*_*nuc 16
派对有点晚了,但是我没有看到这个,所以这里,你也可以将你的json放在一个文件中并通过标准输入使用--file-upload选项将其传递给curl,如下所示:
echo 'my.awesome.json.function({"do" : "whatever"})' | curl -X POST "http://url" -T -
Run Code Online (Sandbox Code Playgroud)
Ami*_*jic 15
这对我有用:
curl -H "Content-Type: application/json" -X POST -d @./my_json_body.txt http://192.168.1.1/json
Run Code Online (Sandbox Code Playgroud)
Ana*_*kzz 15
如果您要包含动态数据,这是另一种方法。
#!/bin/bash
version=$1
text=$2
branch=$(git rev-parse --abbrev-ref HEAD)
repo_full_name=$(git config --get remote.origin.url | sed 's/.*:\/\/github.com\///;s/.git$//')
token=$(git config --global github.token)
generate_post_data()
{
cat <<EOF
{
"tag_name": "$version",
"target_commitish": "$branch",
"name": "$version",
"body": "$text",
"draft": false,
"prerelease": false
}
EOF
}
echo "Create release $version for repo: $repo_full_name branch: $branch"
curl --data "$(generate_post_data)" "https://api.github.com/repos/$repo_full_name/releases?access_token=$token"
Run Code Online (Sandbox Code Playgroud)
Sma*_* Ma 13
Use -d option to add payload
curl -X POST \
http://<host>:<port>/<path> \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"foo": "bar",
"lorem": "ipsum"
}'
Run Code Online (Sandbox Code Playgroud)
In addition:
use -X POST to use POST method
use -H 'Accept: application/json' to add accept type header
use -H 'Content-Type: application/json' to add content type header
小智 9
我使用以下格式来测试Web服务器.
use -F 'json data'
Run Code Online (Sandbox Code Playgroud)
我们假设这个JSON dict格式:
{
'comment': {
'who':'some_one',
'desc' : 'get it'
}
}
Run Code Online (Sandbox Code Playgroud)
curl -XPOST your_address/api -F comment='{"who":"some_one", "desc":"get it"}'
Run Code Online (Sandbox Code Playgroud)
我为此制作了一个名为fetcher的工具。它可以发送请求并格式化 curl 片段:
下面是一个例子:
示例输出:
curl -XGET -H "Accept: application/json" -d "{\"value\":\"30\",\"type\":\"Tip 3\",\"targetModule\":\"Target 3\",\"configurationGroup\":null,\"name\":\"Configuration Deneme 3\",\"description\":null,\"identity\":\"Configuration Deneme 3\",\"version\":0,\"systemId\":3,\"active\":true}" "http://localhost:8080/xx/xxx/xxxx"
Run Code Online (Sandbox Code Playgroud)
对于 PowerShell 我使用过:
curl.exe -H "Content-Type: application/json" --data "@content.json" http://localhost:8080/appname/path
Run Code Online (Sandbox Code Playgroud)
其中content.json是本地包含请求的 JSON 文件的名称,而curl.exe
不是仅仅不使用Invoke-WebRequestcurl
的别名。
或者如果您想直接指定 JSON:
curl.exe -H "Content-Type: application/json" --data '{\"username\":\"xyz\",\"password\":\"xyz\"}' http://localhost:8080/appname/path
Run Code Online (Sandbox Code Playgroud)
小智 5
这在Windows10上对我有用
curl -d "{"""owner""":"""sasdasdasdasd"""}" -H "Content-Type: application/json" -X PUT http://localhost:8080/api/changeowner/CAR4
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2632164 次 |
最近记录: |