我在应用程序中使用Iron Web框架(用于Rust编程语言),并且在使用Router crate时我有一个暴露于POST JSON数据的路径.
它可以工作,但我必须对我的JSON数据进行百分比编码并将其作为字符串附加到我的HTTP POST请求的末尾 - 这有效,但有点乏味,我想最终POST原始图像文件.
我希望能够按照以下curl命令执行某些操作:
curl -v -i --header "Content-Type: application/json" -X POST -d @some_local_json_file.json http://my_server_ip_address:3000/example_path/post/json_object_here
Run Code Online (Sandbox Code Playgroud)
我目前收到一个HTTP/1.1 404 Not Found错误:
curl -v -i --header "Content-Type: application/json" -X POST -d @some_local_json_file.json http://my_server_ip_address:3000/example_path/post/json
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying my_server_ip_address...
* Connected to my_server_ip_address (my_server_ip_address) port 3000 (#0)
> POST /example_path/post/json HTTP/1.1
> Host: my_server_ip_address:3000
> User-Agent: curl/7.45.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 2354
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 404 Not Found
HTTP/1.1 404 Not Found
< Date: Mon, 28 Dec 2015 22:44:03 GMT
Date: Mon, 28 Dec 2015 22:44:03 GMT
< Content-Length: 0
Content-Length: 0
<
* Connection #0 to host my_server_ip_address left intact
Run Code Online (Sandbox Code Playgroud)
我的main功能看起来像:
fn main() {
// create the router
let mut router = Router::new();
router.post("/example_path/post/:json", post_to_documents);
let mut mount = Mount::new();
// mount the router
mount.mount("/", router);
Iron::new(mount).http("0.0.0.0:3000").unwrap();
}
Run Code Online (Sandbox Code Playgroud)
与post_to_documents上面列出的是沿着线:
fn post_to_documents(req: &mut Request) -> IronResult<Response>
{
let document_url_encoded = req.extensions.get::<Router>()
.unwrap()
.find("json")
.unwrap_or("/");
// Just return Ok
Ok(Response::with((status::Ok, "Ok")))
}
Run Code Online (Sandbox Code Playgroud)
我想在document_url_encoded变量中包含JSON数据.(我猜它的命名很差,因为在这种情况下它不会是url/percent编码)
您对HTTP POST的工作原理有误解,或者至少在通过Iron和朋友公开时它是如何工作的.POST请求在URL /路径信息的请求的单独部分中发送,Iron分别公开这两个概念.
您正在使用Iron Router将路径映射到函数并从路径中提取简单参数.您还需要使用Iron Body Parser从POST正文中提取数据.它会自动为您解析JSON,并提供对原始二进制数据的访问.
extern crate iron;
extern crate router;
extern crate mount;
extern crate bodyparser;
use iron::prelude::*;
use iron::status;
use router::Router;
use mount::Mount;
fn post_to_documents(req: &mut Request) -> IronResult<Response> {
match req.extensions.get::<Router>().and_then(|r| r.find("json")) {
Some(name) => println!("The name was {:?}", name),
None => println!("There was no name!"),
}
match req.get::<bodyparser::Json>() {
Ok(Some(json_body)) => println!("Parsed body:\n{:?}", json_body),
Ok(None) => println!("No body"),
Err(err) => println!("Error: {:?}", err)
}
Ok(Response::with((status::Ok, "Ok")))
}
fn main() {
let mut router = Router::new();
router.post("/documents/post/:json", post_to_documents, "new_document");
let mut mount = Mount::new();
mount.mount("/", router);
Iron::new(mount).http("0.0.0.0:3000").unwrap();
}
Run Code Online (Sandbox Code Playgroud)
我有一个名为的文件input.json:
{"key": "value"}
Run Code Online (Sandbox Code Playgroud)
我运行这个命令:
curl -v -i --header "Content-Type: application/json" -X POST -d @input.json http://127.0.0.1:3000/documents/post/awesome
Run Code Online (Sandbox Code Playgroud)
使用服务器的输出:
The name was "awesome"
Parsed body:
Object({"key": String("value")})
Run Code Online (Sandbox Code Playgroud)
我无法解释为什么你会收到404错误.
这是完成的
| 归档时间: |
|
| 查看次数: |
543 次 |
| 最近记录: |