我搜索过,但我找不到答案.我有一个RESTful API来管理基本的CRUD.我正在尝试使用PUT创建更新方法,但我无法检索params值.我正在使用Postman发出请求,我的请求如下:
网址
http://localhost/api/update/987654321
Run Code Online (Sandbox Code Playgroud)
PARAMS
id = 987654321
name = John Smith
age = 35
Run Code Online (Sandbox Code Playgroud)
PHP
$app = new Slim();
$app->put('/update/:id', function( $id ) use( $app ){
var_dump([
'id' => $id,
'name' => $app->request->put('name'),
'age' => $app->request->put('age')
]);
});
Run Code Online (Sandbox Code Playgroud)
我的var_dump()结果是:
array(3) {
["id"]=>
string(9) "987654321"
["name"]=>
NULL
["age"]=>
NULL
}
Run Code Online (Sandbox Code Playgroud)
怎么了?任何的想法?
我正在使用AngularJS $资源模型来REST API.我有这样的事情:
angular.module('libraryapp')
.factory('Book', function($resource){
return $resource('books/:id');
});
Run Code Online (Sandbox Code Playgroud)
我用这种方式:
Book.get({ id: 42 }, function(book) {
console.log(book);
});
Run Code Online (Sandbox Code Playgroud)
但我也想要一个端点到子资源,让我们说:
GET /books/:id/comments
Run Code Online (Sandbox Code Playgroud)
我该如何在模块中定义它?我可以用某种方式扩展Book,就像这样使用它
Book.get({ id: 42 }).Comment.query(function(comments) {
console.log(comments);
});
Run Code Online (Sandbox Code Playgroud) 我有像这样的网络服务
@GET
@Produces("application/json")
@Path("{parameter1}/july/{param2},{param3},{param4}/month")
public Month getResult(@PathParam("parameter1") String parameter1, @PathParam("param2") PathSegment param2 , @PathParam("param3") PathSegment param3, @PathParam("param4") PathSegment param4) {
return action.getResult(parameter1, new Integer(param2.getPath()), new Integer(param3.getPath()), new Integer(param3.getPath()));
}
Run Code Online (Sandbox Code Playgroud)
如果我从我的测试类中调用此Web服务,它可以正常工作; 但如果我通过浏览器调用它,我会收到消息,因为找不到该服务.
我通过浏览器使用的网址是
HTTP://本地主机:8080/Web应用程序/服务/座椅/ mylogin /月/ 1,0,0 /月
如果我使用网址作为
HTTP://本地主机:8080/Web应用程序/服务/座椅/ mylogin /飞/ 1/0/0 /月
并相应地更改服务中的路径它工作正常,但要求是使用逗号而不是斜杠.我们有什么方法可以在网址中使用带逗号分隔参数的web服务?
Guzzle客户端默认从此代码创建
$client->get('https://example.com/{?a}', array('a' => array('c','d')));
Run Code Online (Sandbox Code Playgroud)
这个网址
https://example.com/?a=c,d
Run Code Online (Sandbox Code Playgroud)
在RESTful应用程序中在查询字符串中发送数组的最佳做法是什么?问题是,如何在服务器端确定c,d是字符串还是数组?使用方括号发送数组不是更好a[]=c&a[]=d吗?如何设置Guzzle使用方括号?或者最好使用JSON编码变量?在服务器端,我正在使用Tonic.
我使用Swagger作为我的API工具框架,到目前为止它运作良好.我刚看到这个页面https://petstore.swagger.io/
并看到每个方法如何描述.例如,
POST: pet/描述add a new Pet to the store.我想添加类似[Description("Description text")]应该做的事情,但事实并非如此.我怎样才能做到这一点?
我在概念上理解有关版本化资源的URL的正确方法.
假设我有一个应用程序,它以非常类似于版本控制系统的方式跟踪食谱,就像RCS之类的旧学校一样.每个版本可以是一段时间的工作副本,然后从中创建一个新版本.每个版本都有与之关联的注释,不会共享注释.我可以随时回顾历史并查看配方的演变,但每个实例始终被视为同一配方的版本.我正在尝试找出构建URL以引用这些的最合适的方法,并且我无法理解类似子资源和时间资源之间的一些差异等.
我看到这两个主要方式是:
> 1) query parameters
> -- /recipes/ultimate-thing -> List of available versions of Ultimate Thing
> -- /recipes/ultimate-thing?version=2 -> Version 2 of Ultimate Thing
> -- /recipes/ultimate-thing?version=latest -> Current working version of Ultimate Thing
2a) Nested resources with versions considered subresources
-- /recipes/ultimate-thing/versions/ -> List of available versions of Ultimate Thing
-- /recipes/ultimate-thing/versions/2 -> Version 2 of Ultimate Thing
-- /recipes/ultimate-thing -> Current working version of Ultimate Thing
2b) Nested resources with the list at the resource …Run Code Online (Sandbox Code Playgroud) 不久之后,我用Java开发了一个只有1个GET资源的Restful服务.它是这样访问的:
获取http:// localhost:8080/my-project/customers/transactions
此GET请求返回所有客户事务.
现在,我有另一个项目请求,他们希望在同一个数据库中的不同模式中插入客户事务.我认为不是创建其他服务而是可以增强此服务,因为底层数据库是相同的,而且是关于客户事务.
所以,我在我的服务界面中创建了另一个方法,我createCustomerTransactions想将它命名为与我的GET请求相同,但是这个将是这样的POST:
POST http:// localhost:8080/my-project/customers/transactions
我使用Soap-UI测试了这个并且它可以工作.我的问题是做Restful的正确方法.可以让GET和POST同时具有相同的url,虽然它们会指向不同的实际方法吗?我不擅长名字,所以不能提出另一个更好的资源名称.
我需要从 REST API 中检索 500 部最受欢迎的电影,但结果仅限于每页 20 部,而且我每 10 秒只能进行 40 次调用(https://developers.themoviedb.org/3/getting-启动/请求速率限制)。我无法动态遍历分页结果,因此 500 个最受欢迎的结果位于一个列表中。
我可以成功返回前 20 部最受欢迎的电影(见下文)并枚举电影的编号,但我在循环中陷入困境,该循环允许我分页前 500 部而不会因 API 速率限制而超时。
import requests #to make TMDB API calls
#Discover API url filtered to movies >= 2004 and containing Drama genre_ID: 18
discover_api = 'https://api.themoviedb.org/3/discover/movie?
api_key=['my api key']&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&primary_release_year=>%3D2004&with_genres=18'
#Returning all drama films >= 2004 in popularity desc
discover_api = requests.get(discover_api).json()
most_popular_films = discover_api['results']
#printing movie_id and movie_title by popularity desc
for i, film in enumerate(most_popular_films):
print(i, film['id'], film['title'])
Run Code Online (Sandbox Code Playgroud)
Sample …Run Code Online (Sandbox Code Playgroud) 我正在为我们的后端工作私人api.
我的馆藏有关联.
每个集合都可以请求,分页,您也可以请求关联并对这些关联进行分页.
我们不确定要使用哪种网址设计...我们正在考虑:
/users.json?per_page=10&association=parts,auditions&parts_per_page=5&auditions_per_page=5
/users.json?per_page=10&association[]=parts&association[]=auditions&parts_per_page=5&auditions_per_page=10
/users.json?per_page=10&association[auditions]=true&association[parts][per_page]=5
你怎么看 ?你会选择哪一个?为什么?其中一个看起来不像有效的网址方案?
谢谢 !
当涉及到 Restful URI 上的尾部斜杠时,我可以引用权威立场吗?罗伊菲尔丁的一个会很棒。网络有两种方式的权威意见。这两个位置是: 尾部斜杠表示资源,而没有则没有。另一个论点是尾部斜杠没有语义价值。是哪个?例子:
@GetMapping(path = "/users/")
public List<User> getUsers() {
....
}
@GetMapping(path = "/users/{id}")
public User getUser(@PathVariable String type) {
.....
}
@PutMapping(path = "/users/")
public User updateUser(@RequestBody User user) {
....
}
@PostMapping(path = "/users/")
public User createUser(@RequestBody User user) {
....
}
@DeleteMapping(path = "/users/{id}")
public void deleteUser(@PathVariable Long id) {
....
}
Run Code Online (Sandbox Code Playgroud)
应该删除尾部斜杠吗?
restful-url ×10
rest ×7
jax-rs ×2
php ×2
angularjs ×1
api ×1
c# ×1
guzzle ×1
http ×1
java ×1
put ×1
python ×1
query-string ×1
slim ×1
swagger-ui ×1
swashbuckle ×1
web-services ×1