rest api curl
如果要从命令行快速测试REST api,可以使用curl 。 在这篇文章中,我将介绍如何针对REST API执行GET,POST,PUT,HEAD,DELETE HTTP请求。 出于本博客文章的目的,我将使用在我的帖子教程–借助Jersey和Spring的Java中的REST API设计和实现中开发的REST api。
1.简介
如果在博客文章的第一部分中,我将对curl及其功能(带有选项的HTTP请求)进行简要介绍,那么在第二部分中,我将“翻译”为REST API教程而开发的SOAPui测试套件以进行curl。要求。
好吧, curl是用于使用URL语法传输数据的命令行工具和库,支持DICT,FILE,FTP,FTPS,Gopher,HTTP,HTTPS,IMAP,IMAPS,LDAP,LDAPS,POP3,POP3S,RTMP,RTSP,SCP, SFTP,SMTP,SMTPS,Telnet和TFTP。 curl支持SSL证书,HTTP POST,HTTP PUT,FTP上传,基于HTTP表单的上传,代理,HTTP / 2,cookie,用户+密码验证(基本,摘要,NTLM,协商,Kerberos…),文件传输恢复,代理隧道以及更多。[1]
如前所述,我将使用curl模拟对REST API的HEAD,GET,POST,PUT和DELETE请求调用。
HEAD要求
如果要检查资源是否可服务,提供了什么样的标头以及在响应标头中编写的其他有用的元信息,而不必传输整个内容,则可以发出HEAD请求。 假设我想看看请求播客资源时会得到什么。 我会用curl发出以下HEAD请求:
请求
curl-我发出HEAD请求
curl -I http://localhost:8888/demo-rest-jersey-spring/podcasts/1
要么
针对资源的HEAD请求
curl -i -X HEAD http://localhost:8888/demo-rest-jersey-spring/podcasts/1
卷曲选项
-
-i, --include
--include –在输出中包含协议标头(H / F) -
-X, --request
--request –指定要使用的请求COMMAND(GET,PUT,DELETE…)
响应
HEAD回应
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 631 0 0 0 0 0 0 --:--:-- 0:00:05 --:--:-- 0
HTTP/1.1 200 OK
Date: Tue, 25 Nov 2014 12:54:56 GMT
Server: Jetty(9.0.7.v20131107)
Access-Control-Allow-Headers: X-extra-header
Access-Control-Allow-Headers: X-Requested-With, Content-Type, X-Codingpedia
Allow: OPTIONS
Content-Type: application/xml
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, DELETE, PUT
Vary: Accept-Encoding
Content-Length: 631
请注意以下标题
-
Access-Control-Allow-Headers: Content-Type
-
Access-Control-Allow-Methods: GET, POST, DELETE, PUT
和 -
Access-Control-Allow-Origin: *
在回应中。
已添加它们以支持跨来源资源共享(CORS) 。 您可以在我的文章如何使用Jersey使用Java在服务器端添加CORS支持中找到有关此内容的更多信息。
我发现有点有趣的是响应标头Content-Type: application/xml
,因为我希望它是application/json
,因为在Jersey定义的资源方法中,该标头应该优先:
@产生注释媒体类型
@GET
@Path("{id}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getPodcastById(@PathParam("id") Long id, @QueryParam("detailed") boolean detailed)
throws IOException, AppException {
Podcast podcastById = podcastService.getPodcastById(id);
return Response.status(200)
.entity(podcastById, detailed ? new Annotation[]{PodcastDetailedView.Factory.get()} : new Annotation[0])
.header("Access-Control-Allow-Headers", "X-extra-header")
.allow("OPTIONS").build();
}
GET请求
在URL(资源)上不执行任何参数的curl将会执行GET。
请求
对资源的简单curl调用
curl http://localhost:8888/demo-rest-jersey-spring/podcasts/1
响应
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><podcast><id>1</id><title>- The Naked Scientists Podcast - Stripping Down Science</title><linkOnPodcastpedia>http://www.podcastpedia.org/podcasts/792/-The-Naked-Scientists-Podcast-Stripping-Down-Science</linkOnPodcastpedia><feed>feed_placeholder</feed><description>The Naked Scientists flagship science show brings you a lighthearted look at the latest scientific breakthroughs, interviews with the world top scientists, answers to your science questions and science experiments to try at home.</description><insertionDate>2014-10-29T10:46:02.00+0100</insertionDate></podcast>
请注意,正如预期的HEAD请求所示,我们获得了一个xml文档。 无论如何,我们可以通过在curl请求中添加标题行,并将Accept
HTTP标头设置为application/json
,来强制执行JSON响应:
带有自定义标头的curl请求
curl --header "Accept:application/json" http://localhost:8888/demo-rest-jersey-spring/podcasts/1
卷曲选项
-
-H, --header
--header –传递给服务器的客户标头
带有自定义标头的curl请求
curl -H "Accept:application/json" http://localhost:8888/demo-rest-jersey-spring/podcasts/1
响应
JSON格式的回应
{"id":1,"title":"- The Naked Scientists Podcast - Stripping Down Science","linkOnPodcastpedia":"http://www.podcastpedia.org/podcasts/792/-The-Naked-Scientists-Podcast-Stripping-Down-Science","feed":"feed_placeholder","description":"The Naked Scientists flagship science show brings you a lighthearted look at the latest scientific breakthroughs, interviews with the world top scientists, answers to your science questions and science experiments to try at home.","insertionDate":"2014-10-29T10:46:02.00+0100"}
如果您希望它显示得更漂亮,可以使用以下命令,前提是您的计算机上安装了Python 。
请求
调用带有JSON漂亮打印的资源
curl -H "Accept:application/json" http://localhost:8888/demo-rest-jersey-spring/podcasts/1 | python -m json.tool
响应
JSON响应–精美印刷
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 758 100 758 0 0 6954 0 --:--:-- --:--:-- --:--:-- 6954
[
{
"description": "The Naked Scientists flagship science show brings you a lighthearted look at the latest scientific breakthroughs, interviews with the world top scientists, answers to your science questions and science experiments to try at home.",
"feed": "feed_placeholder",
"id": 1,
"insertionDate": "2014-10-29T10:46:02.00+0100",
"linkOnPodcastpedia": "http://www.podcastpedia.org/podcasts/792/-The-Naked-Scientists-Podcast-Stripping-Down-Science",
"title": "- The Naked Scientists Podcast - Stripping Down Science"
},
{
"description": "Quarks & Co: Das Wissenschaftsmagazin",
"feed": "http://podcast.wdr.de/quarks.xml",
"id": 2,
"insertionDate": "2014-10-29T10:46:13.00+0100",
"linkOnPodcastpedia": "http://www.podcastpedia.org/quarks",
"title": "Quarks & Co - zum Mitnehmen"
}
]
具有多个标头的卷曲请求
正如您在我的最新文章“ 如何使用GZip和Jersey压缩Java REST API中的响应”中所发现的那样,REST api提供的所有响应都已使用GZip进行了压缩。 仅当客户端通过设置以下标头Accept-encoding:gzip
“建议”它接受这种编码时,才会发生这种情况。
请求
使用curl设置多个标题
curl -v -H "Accept:application/json" -H "Accept-encoding:gzip" http://localhost:8888/demo-rest-jersey-spring/podcasts/
卷曲选项
-
-v, --verbose
---v, --verbose
–使操作更健谈
为此,您只需添加另一个具有相应值的-H选项即可。 当然,在这种情况下,如果不将响应重定向到文件,则会在内容中得到一些不可读的字符:
模糊字符响应
* Adding handle: conn: 0x28ddd80
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x28ddd80) send_pipe: 1, recv_pipe: 0
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* About to connect() to proxy vldn680 port 19001 (#0)
* Trying 10.32.142.80...
* Connected to vldn680 (10.32.142.80) port 19001 (#0)
> GET http://localhost:8888/demo-rest-jersey-spring/podcasts/ HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:8888
> Proxy-Connection: Keep-Alive
> Accept:application/json
> Accept-encoding:gzip
>
< HTTP/1.1 200 OK
< Date: Tue, 25 Nov 2014 16:17:02 GMT
* Server Jetty(9.0.7.v20131107) is not blacklisted
< Server: Jetty(9.0.7.v20131107)
< Content-Type: application/json
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, POST, DELETE, PUT
< Access-Control-Allow-Headers: X-Requested-With, Content-Type, X-Codingpedia
< Vary: Accept-Encoding
< Content-Encoding: gzip
< Content-Length: 413
< Via: 1.1 vldn680:8888
<
{ [data not shown]
100 413 100 413 0 0 2647 0 --:--:-- --:--:-- --:--:-- 2647▒QKo▒0▒+▒▒g▒▒R▒+{▒V▒Pe▒▒؊c▒▒ n▒▒▒▒fæHH▒"▒▒g▒/?2▒eM▒gl▒a▒d
▒{=`7▒Eϖ▒▒c▒ZM
n8▒i▒▒▒}H▒▒i1▒3g▒▒▒▒▒ ;▒E▒0O▒n▒R*▒g/E▒▒n=▒▒▒▒)▒U▒▒▒lժ▒Φ▒h▒6▒▒▒_>w▒▒-▒▒:▒▒▒!▒Bb▒Z▒▒tO▒N@'= |▒▒C▒f▒▒loؠ▒,T▒▒A▒4▒▒:▒l+#▒0b!▒▒'▒G▒^▒Iﺬ.TU▒▒▒z▒\▒i^]e▒▒▒▒2▒▒▒֯▒▒?▒:/▒m▒▒▒▒▒Y▒h▒▒▒_䶙V▒+R▒WT▒0▒?f{▒▒▒▒&▒l▒▒Sk▒iԽ~▒▒▒▒▒▒n▒▒▒▒_V]į▒
* Connection #0 to host vldn680 left intact
2. SOAPui测试套件转换为curl请求
如前所述,在第二部分中,我将映射到curl请求, 这里介绍了SOAPui测试套件。
创建播客资源
删除所有播客(准备步骤)
请求
删除所有播客
curl -i -X DELETE http://localhost:8888/demo-rest-jersey-spring/podcasts/
响应
HTTP/1.1 204 No Content
Date: Tue, 25 Nov 2014 14:10:17 GMT
Server: Jetty(9.0.7.v20131107)
Content-Type: text/html
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, DELETE, PUT
Access-Control-Allow-Headers: X-Requested-With, Content-Type, X-Codingpedia
Vary: Accept-Encoding
Via: 1.1 vldn680:8888
Content-Length: 0
POST不带提要的新播客– 400(BAD_REQUEST)
请求
curl -i -X POST -H "Content-Type:application/json" http://localhost:8888/demo-rest-jersey-spring/podcasts/ -d '{"title":"- The Naked Scientists Podcast - Stripping Down Science-new-title2","linkOnPodcastpedia":"http://www.podcastpedia.org/podcasts/792/-The-Naked-Scientists-Podcast-Stripping-Down-Science","description":"The Naked Scientists flagship science show brings you a lighthearted look at the latest scientific breakthroughs, interviews with the world top scientists, answers to your science questions and science experiments to try at home."}'
响应
响应400错误请求
HTTP/1.1 400 Bad Request
Date: Tue, 25 Nov 2014 15:12:11 GMT
Server: Jetty(9.0.7.v20131107)
Content-Type: application/json
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, DELETE, PUT
Access-Control-Allow-Headers: X-Requested-With, Content-Type, X-Codingpedia
Vary: Accept-Encoding
Content-Length: 271
Via: 1.1 vldn680:8888
Connection: close
{"status":400,"code":400,"message":"Provided data not sufficient for insertion","link":"http://www.codingpedia.org/ama/tutorial-rest-api-design-and-implementation-in-java-with-jersey-and-spring/","developerMessage":"Please verify that the feed is properly generated/set"}
正确发布新播客– 201(已创建)
请求
正确发布新播客– 201(已创建)
curl -i -X POST -H "Content-Type:application/json" http://localhost:8888/demo-rest-jersey-spring/podcasts/ -d '{"title":"- The Naked Scientists Podcast - Stripping Down Science","linkOnPodcastpedia":"http://www.podcastpedia.org/podcasts/792/-The-Naked-Scientists-Podcast-Stripping-Down-Science","feed":"feed_placeholder","description":"The Naked Scientists flagship science show brings you a lighthearted look at the latest scientific breakthroughs, interviews with the world top scientists, answers to your science questions and science experiments to try at home."}'
响应
curl -i -X POST -H "Content-Type:application/json" http://localhost:8888/demo-rest-jersey-spring/podcasts/ -d '{"title":"- The Naked Scientists Podcast - Stripping Down Science-new-title2","linkOnPodcastpedia":"http://www.podcastpedia.org/podcasts/792/-The-Naked-Scientists-Podcast-Stripping-Down-Science","description":"The Naked Scientists flagship science show brings you a lighthearted look at the latest scientific breakthroughs, interviews with the world top scientists, answers to your science questions and science experiments to try at home."}'
发布与以前相同的播客以接收– 409(CONFLICT)
请求
curl -i -X POST -H "Content-Type:application/json" http://localhost:8888/demo-rest-jersey-spring/podcasts/ -d '{"title":"- The Naked Scientists Podcast - Stripping Down Science","linkOnPodcastpedia":"http://www.podcastpedia.org/podcasts/792/-The-Naked-Scientists-Podcast-Stripping-Down-Science","feed":"feed_placeholder","description":"The Naked Scientists flagship science show brings you a lighthearted look at the latest scientific breakthroughs, interviews with the world top scientists, answers to your science questions and science experiments to try at home."}'
响应
HTTP/1.1 409 Conflict
Date: Tue, 25 Nov 2014 15:58:39 GMT
Server: Jetty(9.0.7.v20131107)
Content-Type: application/json
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, DELETE, PUT
Access-Control-Allow-Headers: X-Requested-With, Content-Type, X-Codingpedia
Vary: Accept-Encoding
Content-Length: 300
{"status":409,"code":409,"message":"Podcast with feed already existing in the database with the id 1","link":"http://www.codingpedia.org/ama/tutorial-rest-api-design-and-implementation-in-java-with-jersey-and-spring/","developerMessage":"Please verify that the feed and title are properly generated"}
在位置放置新播客– 201(已创建)
请求
curl -i -X PUT -H "Content-Type:application/json" http://localhost:8888/demo-rest-jersey-spring/podcasts/2 -d '{"id":2,"title":"Quarks & Co - zum Mitnehmen","linkOnPodcastpedia":"http://www.podcastpedia.org/quarks","feed":"http://podcast.wdr.de/quarks.xml","description":"Quarks & Co: Das Wissenschaftsmagazin"}'
响应
HTTP/1.1 201 Created
Location: http://localhost:8888/demo-rest-jersey-spring/podcasts/2
Content-Type: text/html
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, DELETE, PUT
Access-Control-Allow-Headers: X-Requested-With, Content-Type, X-Codingpedia
Vary: Accept-Encoding
Content-Length: 60
Server: Jetty(9.0.7.v20131107)
A new podcast has been created AT THE LOCATION you specified
阅读播客资源
获取新插入的播客– 200(确定)
请求
curl -v -H "Accept:application/json" http://localhost:8888/demo-rest-jersey-spring/podcasts/1 | python -m json.tool
响应
< HTTP/1.1 200 OK
< Access-Control-Allow-Headers: X-extra-header
< Access-Control-Allow-Headers: X-Requested-With, Content-Type, X-Codingpedia
< Allow: OPTIONS
< Content-Type: application/json
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, POST, DELETE, PUT
< Vary: Accept-Encoding
< Content-Length: 192
* Server Jetty(9.0.7.v20131107) is not blacklisted
< Server: Jetty(9.0.7.v20131107)
<
{ [data not shown]
* STATE: PERFORM => DONE handle 0x600056180; line 1626 (connection #0)
100 192 100 192 0 0 2766 0 --:--:-- --:--:-- --:--:-- 3254
* Connection #0 to host localhost left intact
* Expire cleared
{
"feed": "http://podcast.wdr.de/quarks.xml",
"id": 1,
"insertionDate": "2014-06-05T22:35:34.00+0200",
"linkOnPodcastpedia": "http://www.podcastpedia.org/quarks",
"title": "Quarks & Co - zum Mitnehmen"
}
GET播客,按插入日期DESC排序-200(确定)
请求
curl -v -H "Accept:application/json" http://localhost:8888/demo-rest-jersey-spring/podcasts?orderByInsertionDate=DESC | python -m json.tool
响应
漂亮的格式回复
< HTTP/1.1 200 OK
< Content-Type: application/json
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, POST, DELETE, PUT
< Access-Control-Allow-Headers: X-Requested-With, Content-Type, X-Codingpedia
< Vary: Accept-Encoding
< Content-Length: 419
* Server Jetty(9.0.7.v20131107) is not blacklisted
< Server: Jetty(9.0.7.v20131107)
<
0 419 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0{ [data not shown]
* STATE: PERFORM => DONE handle 0x600056180; line 1626 (connection #0)
100 419 100 419 0 0 6044 0 --:--:-- --:--:-- --:--:-- 6983
* Connection #0 to host localhost left intact
* Expire cleared
[
{
"feed": "http://podcast.wdr.de/quarks.xml",
"id": 1,
"insertionDate": "2014-06-05T22:35:34.00+0200",
"linkOnPodcastpedia": "http://www.podcastpedia.org/quarks",
"title": "Quarks & Co - zum Mitnehmen"
},
{
"feed": "http://www.dayintechhistory.com/feed/podcast-2",
"id": 2,
"insertionDate": "2014-06-05T22:35:34.00+0200",
"linkOnPodcastpedia": "http://www.podcastpedia.org/podcasts/766/Day-in-Tech-History",
"title": "Day in Tech History"
}
]
更新播客资源
PUT未“完成”播客以进行全面更新– 400(BAD_REQUEST)
请求
curl -v -H "Content-Type:application/json" -X PUT http://localhost:8888/demo-rest-jersey-spring/podcasts/2 -d '{"id":2, "title":"Quarks & Co - zum Mitnehmen","linkOnPodcastpedia":"http://www.podcastpedia.org/quarks","feed":"http://podcast.wdr.de/quarks.xml"}'
响应
< HTTP/1.1 400 Bad Request
< Content-Type: application/json
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, POST, DELETE, PUT
< Access-Control-Allow-Headers: X-Requested-With, Content-Type, X-Codingpedia
< Vary: Accept-Encoding
< Content-Length: 290
* Server Jetty(9.0.7.v20131107) is not blacklisted
< Server: Jetty(9.0.7.v20131107)
<
* STATE: PERFORM => DONE handle 0x600056180; line 1626 (connection #0)
* Connection #0 to host localhost left intact
* Expire cleared
{"status":400,"code":400,"message":"Please specify all properties for Full UPDATE","link":"http://www.codingpedia.org/ama/tutorial-rest-api-design-and-implementation-in-java-with-jersey-and-spring/","developerMessage":"required properties - id, title, feed, lnkOnPodcastpedia, description"}
进行完整更新的PUT播客– 200(确定)
请求
$ curl -v -H "Content-Type:application/json" -X PUT http://localhost:8888/demo-rest-jersey-spring/podcasts/2 -d '{"id":2, "title":"Quarks & Co - zum Mitnehmen","linkOnPodcastpedia":"http://www.podcastpedia.org/quarks","feed":"http://podcast.wdr.de/quarks.xml", "description":"Quarks & Co: Das Wissenschaftsmagazin"}'
响应
< HTTP/1.1 200 OK
< Location: http://localhost:8888/demo-rest-jersey-spring/podcasts/2
< Content-Type: text/html
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, POST, DELETE, PUT
< Access-Control-Allow-Headers: X-Requested-With, Content-Type, X-Codingpedia
< Vary: Accept-Encoding
< Content-Length: 86
* Server Jetty(9.0.7.v20131107) is not blacklisted
< Server: Jetty(9.0.7.v20131107)
<
* STATE: PERFORM => DONE handle 0x600056180; line 1626 (connection #0)
* Connection #0 to host localhost left intact
* Expire cleared
The podcast you specified has been fully updated created AT THE LOCATION you specified
不存在的播客的POST(部分更新)– 404(NOT_FOUND)
请求
$ curl -v -H "Content-Type:application/json" -X POST http://localhost:8888/demo-rest-jersey-spring/podcasts/3 -d '{"title":"Quarks & Co - zum Mitnehmen - GREAT PODCAST"}' | python -m json.tool
响应
< HTTP/1.1 404 Not Found
< Content-Type: application/json
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, POST, DELETE, PUT
< Access-Control-Allow-Headers: X-Requested-With, Content-Type, X-Codingpedia
< Vary: Accept-Encoding
< Content-Length: 306
* Server Jetty(9.0.7.v20131107) is not blacklisted
< Server: Jetty(9.0.7.v20131107)
<
{ [data not shown]
* STATE: PERFORM => DONE handle 0x600056180; line 1626 (connection #0)
100 361 100 306 100 55 9069 1630 --:--:-- --:--:-- --:--:-- 13304
* Connection #0 to host localhost left intact
* Expire cleared
{
"code": 404,
"developerMessage": "Please verify existence of data in the database for the id - 3",
"link": "http://www.codingpedia.org/ama/tutorial-rest-api-design-and-implementation-in-java-with-jersey-and-spring/",
"message": "The resource you are trying to update does not exist in the database",
"status": 404
}
POST(部分更新)播客– 200(确定)
请求
$ curl -v -H "Content-Type:application/json" -X POST http://localhost:8888/demo-rest-jersey-spring/podcasts/2 -d '{"title":"Quarks & Co - zum Mitnehmen - GREAT PODCAST"}'
响应
< HTTP/1.1 200 OK
< Content-Type: text/html
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, POST, DELETE, PUT
< Access-Control-Allow-Headers: X-Requested-With, Content-Type, X-Codingpedia
< Vary: Accept-Encoding
< Content-Length: 55
* Server Jetty(9.0.7.v20131107) is not blacklisted
< Server: Jetty(9.0.7.v20131107)
<
* STATE: PERFORM => DONE handle 0x600056180; line 1626 (connection #0)
* Connection #0 to host localhost left intact
* Expire cleared
The podcast you specified has been successfully updated
删除资源
删除第二个插入的播客– 204(NO_CONTENT)
请求
$ curl -v -X DELETE http://localhost:8888/demo-rest-jersey-spring/podcasts/2
响应
< HTTP/1.1 204 No Content
< Content-Type: text/html
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, POST, DELETE, PUT
< Access-Control-Allow-Headers: X-Requested-With, Content-Type, X-Codingpedia
< Vary: Accept-Encoding
* Server Jetty(9.0.7.v20131107) is not blacklisted
< Server: Jetty(9.0.7.v20131107)
<
* Excess found in a non pipelined read: excess = 42 url = /demo-rest-jersey-spring/podcasts/2 (zero-length body)
* STATE: PERFORM => DONE handle 0x600056180; line 1626 (connection #0)
* Connection #0 to host localhost left intact
* Expire cleared
GET删除播客– 404(NOT_FOUND)
请求
curl -v http://localhost:8888/demo-rest-jersey-spring/podcasts/2 | python -m json.tool
响应
< HTTP/1.1 404 Not Found
< Content-Type: application/json
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, POST, DELETE, PUT
< Access-Control-Allow-Headers: X-Requested-With, Content-Type, X-Codingpedia
< Vary: Accept-Encoding
< Content-Length: 306
* Server Jetty(9.0.7.v20131107) is not blacklisted
< Server: Jetty(9.0.7.v20131107)
<
{ [data not shown]
* STATE: PERFORM => DONE handle 0x600056180; line 1626 (connection #0)
100 306 100 306 0 0 8916 0 --:--:-- --:--:-- --:--:-- 13304
* Connection #0 to host localhost left intact
* Expire cleared
{
"code": 404,
"developerMessage": "Verify the existence of the podcast with the id 2 in the database",
"link": "http://www.codingpedia.org/ama/tutorial-rest-api-design-and-implementation-in-java-with-jersey-and-spring/",
"message": "The podcast you requested with id 2 was not found in the database",
"status": 404
}
奖励操作
从urlencoded的申请表中添加播客
请求
带有urlencoded的POST
curl -v --data-urlencode "title=Day in Tech History" --data-urlencode "linkOnPodcastpedia=http://www.podcastpedia.org/podcasts/766/Day-in-Tech-History" --data-urlencode "feed=http://www.dayintechhistory.com/feed/podcast"
响应
< HTTP/1.1 201 Created
< Location: http://localhost:8888/demo-rest-jersey-spring/podcasts/null
< Content-Type: text/html
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, POST, DELETE, PUT
< Access-Control-Allow-Headers: X-Requested-With, Content-Type, X-Codingpedia
< Vary: Accept-Encoding
< Content-Length: 81
* Server Jetty(9.0.7.v20131107) is not blacklisted
< Server: Jetty(9.0.7.v20131107)
<
* STATE: PERFORM => DONE handle 0x600056180; line 1626 (connection #0)
* Connection #0 to host localhost left intact
* Expire cleared
A new podcast/resource has been created at /demo-rest-jersey-spring/podcasts/null
注意:
我仍处于使用curl的开始阶段,因此,如果您有任何建议,请发表评论。 谢谢。
资源资源
翻译自: https://www.javacodegeeks.com/2014/12/how-to-test-a-rest-api-from-command-line-with-curl.html
rest api curl