我可以看到这是如何工作的:
/用户/ 456
使用GET,POST和DELETE而不是PUT,除非调用者以某种方式知道下一个主键或者他们自己提供它......这是怎么做到的?
我将按照我在此处阅读的内容进行操作:在REST中使用PUT与POST
PUT方法请求将所包含的实体存储在提供的Request-URI下.
我通过API中的REST CLIENT发送的PUT和POST请求之间存在差异.它在CodeIgniter中与Phil Sturgeon的REST服务器一起实现.
function station_put(){
$data = array(
'name' => $this->input->post('name'),
'number' => $this->input->post('number'),
'longitude' => $this->input->post('longitude'),
'lat' => $this->input->post('latitude'),
'typecode' => $this->input->post('typecode'),
'description' => $this->input->post('description'),
'height' => $this->input->post('height'),
'mult' => $this->input->post('mult'),
'exp' => $this->input->post('exp'),
'elevation' => $this->input->post('elevation')
);
$id_returned = $this->station_model->add_station($data);
$this->response(array('id'=>$id_returned,'message'=>"Successfully created."),201);
}
Run Code Online (Sandbox Code Playgroud)
此请求成功地将数据插入到服务器BUT中 - 除了id之外,它将其余值呈现为NULL.
但是,如果将函数名称更改为station_post,则会正确插入数据.
有人请指出为什么PUT请求不起作用?我使用的是最新版本的谷歌浏览器.
顺便说一下,这个API将集成到BackBone处理的应用程序中.我真的需要使用PUT吗?或者在使用post时,是否还有另一种使用骨干模型保存功能的解决方法?
Request method 'PUT' not supported我在使用Restful API 上的PUT方法上传文件时遇到错误。
以下是上传到此票证的信息。
$ curl -X PUT -T "/cygdrive/c/a/documents/test.pptx" http://localhost:8080/piranha-storage-service/buckets/gnaval.bucket1/test.pptx
<html><head><title>Apache Tomcat/7.0.52 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 405 - Request method 'PUT' not supported</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Request method 'PUT' not supported</u></p><p><b>description</b> <u>The specified HTTP method is not allowed for the requested …Run Code Online (Sandbox Code Playgroud) 根据这篇文章(http://restcookbook.com/HTTP%20Methods/put-vs-post/),PUT应该作为更新资源的方法.
但是,使用JAX_RS 2.0和Jersey 2.0练习RESTful,我认为它不会更新特定资源.(即我正在使用JAX_RS 2.0和Jersey 2.0学习RESTful)
这是一个这样的资源.
<customer>
<name>Before</name>
<postcode>111</postcode>
</customer>
Run Code Online (Sandbox Code Playgroud)
我想要做的是更新(也许我应该说"替换")这个资源.
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target("http://xxx/yyy/zzz/end.cust");
Customer cust = new Customer();
cust.setName("After");
cust.setPostcode(222);
target.path("Before").request().put(Entity.xml(cust));
Run Code Online (Sandbox Code Playgroud)
@Id注释在"Customer"类中设置为"Name",因此路径"Before"应该作为ID工作,第一个资源(名为"Before")应该替换为第二个资源(名为"After") ").
但是,在执行上面的编码之后,"之前"资源仍然存在,并且有一个新的"After"resrouce.似乎PUT方法可以创建一个新资源,而不是更新某些东西.(即有"之前"和"之后"资源,并且没有更新任何内容)
我测试了一个POST方法以创建一个新资源,并按照我的预期创建了一个新资源.
如果你看到我做错了什么或需要做什么,你能提出一些建议吗?
编辑
我将添加服务器端代码.用@PUT注释的方法是这样的.
@PUT
@Path("{id}")
@Consumes({"application/xml", "application/json"})
public void edit(@PathParam("id") String id, Customer entity) {
super.edit(entity);
}
Run Code Online (Sandbox Code Playgroud)
这是在一个名为CustomerFacadeREST.java的类中,在我创建"来自数据库的RESTful服务"后自动创建.
根据NetBeans的文档,super.edit()方法最初是这样的.
public void edit(T entity) {
getEntityManager().merge(entity);
}
Run Code Online (Sandbox Code Playgroud)
在"Customer"类中,@ Id以这种方式设置为"name"值.
public class Customer implements Serializable {
private static final long serialVersionUID = 1L; …Run Code Online (Sandbox Code Playgroud) 我正在使用 Flask 和 Flask-Restful 创建 RESTful API,但遇到了 PUT 请求和参数的问题。我有一个 JSON 文件,其中包含:
{
"checked": 1465628401,
"number": 21600,
}
Run Code Online (Sandbox Code Playgroud)
当用户发送时,"/api/v1/status?checked=10"它会像应有的那样更改检查的值,但将number设置为 null。我想知道是否有一种方法可以拥有它,这样如果用户没有指定该参数,它就保持其当前值。
现在,我将 RequestParser 定义为:
parser = reqparse.RequestParser()
Run Code Online (Sandbox Code Playgroud)
并添加每个参数,例如:
parser_status.add_argument('checked', type=int)
parser_status.add_argument('number', type=int)
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,有没有办法默认每个 JSON 键保持其当前值,除非 PUT 请求中存在参数?
文档指出:另请注意:在请求解析器中声明但未在请求本身中设置的参数将默认为 None。我想知道是否有办法改变默认值。
如何在 Java 中使用 JSONObject 将键的值设置为整数?我可以使用设置字符串值JSONObject.put(a,b);
但是,我无法弄清楚如何使用.put()设置整数值。例如:我希望我的 jsonobject 看起来像这样:
{"age": 35}
而不是
{"age": "35"}.
如果客户端尝试更新资源时 URI 的 id 与正文的 id 不同,那么响应应该是什么?EG :
URI:
PUT /members/123
Run Code Online (Sandbox Code Playgroud)
身体
{
id : 456,
name : "john"
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 curl 的 put 请求向本地服务器发出 put 请求:
curl -X PUT -H "Content-Type: application/json" -d '{"connid":"12"}' "127.0.0.1:8000/api/kill"
Run Code Online (Sandbox Code Playgroud)
我收到相同的回复:
'WSGIRequest' 对象没有属性 'PUT'
对于以下代码:
def kill(req):
conid = req.PUT['connid']
statusres = {}
if conid in state:
error[conid] = 'true'
statusres['status'] = 'ok'
else:
statusres['status'] = 'invalid connection Id : '+ conid
return JsonResponse(statusres)
Run Code Online (Sandbox Code Playgroud)
我@csrf_exempt之前也用过这个函数。
假设有人PUT对我的Endint 执行请求:
/resources/{id}
Run Code Online (Sandbox Code Playgroud)
但是我的PostgreSQL数据库中没有存储具有给定ID的资源。
根据RFC 2616,如果我能够执行以下操作,则应创建资源:
该
PUT方法请求将封闭的实体存储在提供的Request-URI下。如果Request-URI引用了已经存在的资源,则应将封闭的实体视为驻留在原始服务器上的实体的修改版本。如果Request-URI没有指向现有资源,并且请求用户代理能够将该URI定义为新资源,则原始服务器可以使用该URI创建资源。
可以使用提供的ID创建资源吗?由于在数据库插入上手动分配ID并不是最佳实践。
如果404无法创建资源,是否应该返回错误?
阅读此 SO链接后,
PUT 最常用于更新功能,PUT 到已知资源 URI,请求正文包含原始资源的最新更新表示。
...我们需要再次发送数据的所有参数。
在我的控制器中,我有:
$student = Student::find($student_id);
$student->username = $request->username;
$student->email = $request->email;
$student->password = $request->password;
$path = $request->file('passport')->store('upload');
$student->passport = $path;
Run Code Online (Sandbox Code Playgroud)
我曾经对POST方法使用过相同的代码并且它有效,但是在将它用于 API 时,我使用了 POSTMANform-data并且必须$request->all()是null. 有人说我应该使用,x-www-form-urlencoded但这不允许文件上传。
put ×10
rest ×6
http ×2
java ×2
json ×2
python ×2
api ×1
codeigniter ×1
django ×1
flask ×1
http-method ×1
integer ×1
jax-rs ×1
jsonobject ×1
laravel ×1
php ×1
spring-boot ×1
url ×1
wsgi ×1