该方法现在如下所示:
POST person/personId/edit
https://api.example.com/*key*/person/*personId*/edit?FName=Blah
Run Code Online (Sandbox Code Playgroud)
我希望将 personId 中人员的名字更改为 Blah。
如果我需要添加一个人,我会说:
PUT person/create
https://api.example.com/*key*/person/create
Run Code Online (Sandbox Code Playgroud)
它会添加一个具有新 personId 的人。
使用具有签名、到期日和访问密钥的预签名 URL 将文件上传到 Amazon S3,使用以下代码我可以使用普通的 java 代码上传文件,但在 Android 中相同的代码给了我 403 错误。使用 Amazon SDK 生成预签名 URL
我已阅读http://developer.android.com/reference/java/net/HttpURLConnection.html 和http://android-developers.blogspot.com/2011/09/androids-http-clients.html但无法阅读弄清楚我应该使用什么标头参数,我猜在 android 中它是在服务器拒绝的请求中设置标头
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("This text uploaded as object.");
out.close();
int responseCode = connection.getResponseCode();
Run Code Online (Sandbox Code Playgroud)
异常:403;签名不匹配:-o
有没有人遇到过这个问题?或者更多关于从android库在幕后添加哪些Header参数的细节?
我可以在swift中与此代码段同步连接到服务器.
let URL: NSURL = NSURL(string: "http://someserver.com)!
let InfoJSON: NSData? = NSData(contentsOfURL: URL)
let JsonInfo: NSString = NSString(data:InfoJSON!, encoding: NSUTF8StringEncoding)!
let GameListAttributions: NSArray = NSJSONSerialization.JSONObjectWithData(InfoJSON!, options: .allZeros, error: nil)! as NSArray
Run Code Online (Sandbox Code Playgroud)
这仅适用于一次接收所有信息,但我如何使用Swift使用GET,POST和PUT.无论我搜索多少,我都找不到关于如何执行这些的好教程或示例.
我相信问题在于提交对数据库的更改(倒数第三行:)db.session.commit()。以一个用户为例:username="Foo", email="Bar@yahoo.com"。如果在 PUT 请求正文中我放了 {"email":"changed@gmail.com"},则在赋值后打印 'user.email' 表明该值实际上已更改。然而之后,在查询数据库时,电子邮件保持不变。无论如何,我真的很难弄清楚我错过了什么,所以感谢任何帮助!
class UserAPI(Resource):
def __init__(self):
self.parser = reqparse.RequestParser()
self.parser.add_argument('username', type=str, location='json')
self.parser.add_argument('email', type=str, location='json')
self.parser.add_argument('password', type=str, location='json')
super(UserAPI, self).__init__()
def put(self, id):
# Not working
user = User.query.filter_by(id=id).first()
if user is not None:
args = self.parser.parse_args()
for key, value in args.items():
if args[key] is not None:
user.key = value
db.session.commit()
return {"user": marshal(user,user_field) }
return {"error": "User not found"}, 404
Run Code Online (Sandbox Code Playgroud) HTTP/WebDav规范是否允许此客户端 - 服务器对话框?
注意:此PUT是初始上传.这不是更新.
如果可以,则可以实现更快的文件同步.
使用案例:WebDAV服务器为每个用户托管一个目录.最喜欢的视频foo.mkv由多个用户上传.在此示例中,收藏的视频已存储在此位置:/user2/myfoo.mkv.第二次和以后的上传不需要发送任何数据,因为服务器已经知道内容.这会减少很多网络负载.
前提条件:
在自定义客户端和服务器中实现它非常容易.但这不是我想要的.
我的问题:是否有允许这样的对话的RFC或其他标准?
如果还没有标准,那么如何实现这个梦想呢?
安全考虑
通过上面的对话框,它将能够访问已知哈希的内容.恶意客户端的示例知道存在哈希值为的文件1234567....他可以执行上述两个步骤,之后客户端可以使用GET下载数据.
解决此问题的方法是扩展对话框:
abcde...怎么做到这一点?
由于似乎还没有规范,问题的这一部分仍然存在:
如何实现梦想成真?
当我通过 curl 将 PUT 请求 API 发送到 REST 时,我发现了奇怪的行为。如果设置参数curl_setopt($curl, CURLOPT_PUT, true),则查询,其中 CURLOPT_POSTFIELDS 不为空,则查询执行持续 1.5 分钟(好像它取决于某个超时)。如果使用参数curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT")发送相同的请求,那么查询执行将持续大约 1 秒,这是应该的。有人可以解释这些参数之间的根本区别吗?
示例代码:
$data = http_build_query(array("enable"=> 1));
if( $curl = curl_init() ) {
curl_setopt($curl, CURLOPT_URL, BASE_URL .'users/2');
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_PUT, true); // execution time 1.5 min
//curl_setopt ($ curl, CURLOPT_CUSTOMREQUEST, "PUT"); - execution time 1 sec
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$out = json_decode(curl_exec($curl));
curl_close($curl);
}
Run Code Online (Sandbox Code Playgroud) 我正在使用API构建应用程序,到目前为止,所有http请求(get,post)都工作正常。现在,我向我的服务中添加了一个put请求,由于某种原因,它不起作用。
这是我所谓的服务功能:
export class ChatStartComponent implements OnInit {
@ViewChild('d1') d1:ElementRef;
socket = io.connect('http://localhost:3000');
constructor(private usersService: UsersService) {
this.currentUser = JSON.parse(localStorage.getItem('currentUser'));
this.currentUserName = this.currentUser.name;
this.usersService.updateUser(this.currentUser._id); <--update Users does not
/*this.usersService.getUsers() <-- getUsers does work
.subscribe(users => {
this.users.push(users);
});*/
}
Run Code Online (Sandbox Code Playgroud)
参数currentUser._id确实存在,我可以在此时控制台对其进行登录。
这是服务功能:
addUser(newUser) {
var headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.post('http://localhost:3000/api/user', JSON.stringify(newUser), {headers: headers});
}
updateUser(_id: string) {
var set = {
loggedIn: true
};
console.log(set, _id);
return this.http.put('http://localhost:3000/api/update/' + _id, JSON.stringify(set))
.map(res => res.json());
}
Run Code Online (Sandbox Code Playgroud)
上面的http.post调用工作正常,但放置的updateUser根本不工作。console.log被触发了,我确实记录了ID,但http.put调用不起作用。
这是服务器端代码:
router.put('/update/:id', …Run Code Online (Sandbox Code Playgroud) 我不知道 PATCH 和 PUT 方法之间的确切区别。谁能告诉我什么时候应该使用这种方法并举出适当的例子。
对于不是由HTML表单发送的请求,HTTP是否将请求的Content-Type限制为application/x-www-form-urlencoded以用于非文件上载,或者是MIME类型"right"/标准/语义在任何其他方面有意义办法?
例如,PHP会自动将内容解析为$ _POST,这似乎表明服务器需要x-www-form-urlencoded.另一方面,我可以使用Ajax在HTTP请求内容中发送JSON对象,并将Content-Type设置为application/json.至少一些服务器技术(例如WSGI)不会尝试解析它,而是以原始形式向脚本提供它.
我应该在RESTful API中的POST和PUT请求中使用什么MIME类型来确保符合HTTP的所有服务器实现?我忽略了SOAP和JSON-RPC等技术,因为它们通过HTTP隧道传输协议而不是按预期使用HTTP.
我写了这个简单的程序:
@app.route('/puttest/', methods=['GET', 'PUT'])
def upload_file():
if request.method == 'PUT':
return 'Hello, {}!'.format(request.form['name'])
else:
return '''
<title>Does it work ?</title>
<h1>PUT test</h1>
<form action=http://localhost:8887/puttest/ method=put>
<input type=text name=name>
<input type=submit value=try>
</form>
'''
if __name__ == '__main__':
app.run('0.0.0.0', 8887)
Run Code Online (Sandbox Code Playgroud)
它适用于GET方法,但它无法使用PUT.尝试发送put消息,我可以在浏览器中看到此错误:
Method Not Allowed
The method is not allowed for the requested URL.
Run Code Online (Sandbox Code Playgroud)
put方法发生了什么?
如果我在程序中的任何地方改变put方法,它将工作正常post.