【问题标题】:Is possible to post a JSON file at a router location with the Iron framework?是否可以使用 Iron 框架在路由器位置发布 JSON 文件?
【发布时间】:2017-06-16 01:18:22
【问题描述】:

我在一个应用程序中使用 Iron Web 框架(用于 Rust 编程语言),并且我在使用 Router crate 时有一个暴露给 POST JSON 数据的路径。

它可以工作,但我必须对我的 JSON 数据进行百分比编码并将其作为字符串附加到我的 HTTP 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

我目前收到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

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();
}

上面列出的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")))
}

我想在 document_url_encoded 变量中保存 JSON 数据。 (我猜它的名字不好,因为在这种情况下它不会是 url/percent 编码)

【问题讨论】:

    标签: json rust iron


    【解决方案1】:

    您对 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();
    }
    

    我有一个名为input.json的文件:

    {"key": "value"}
    

    然后我运行这个命令:

    curl -v -i --header "Content-Type: application/json" -X POST -d @input.json http://127.0.0.1:3000/documents/post/awesome
    

    使用来自服务器的输出:

    The name was "awesome"
    Parsed body:
    Object({"key": String("value")})
    

    我无法解释为什么您会收到 404 错误。

    这是用

    完成的
    • bodyparser 0.7.0
    • 铁 0.5.1
    • 安装 0.3.0
    • 路由器 0.5.1

    【讨论】:

    • 嗯,有道理。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 2016-03-14
    • 2016-06-03
    • 2016-12-16
    • 2017-10-05
    相关资源
    最近更新 更多