#[ ACTIX - WEB ] Postman returns 404 [ SOLVED ]

48 messages · Page 1 of 1 (latest)

cosmic pewter
#

I've code like this

 let scope = web::scope("/api")
        .service(web::resource("/get/{path}/{id}").to(get_data))

and this

pub async fn get_data(
    db: web::Data<libsql::Database>,
    path: web::Path<(Option<String>, Option<String>)>,
) -> Result<HttpResponse, Error> {
    //println!("AAAAA {:?}", &path);
    let (mut linkpath, mut id) = (Option::None, Option::None);
    match id {
        Some(_) => (linkpath, id) = path.into_inner(),
        _ =>  linkpath = path.into_inner().0,
    } //let (path, id) = path.into_inner();
    let conn = db.connect().expect(" Get Data DataBase Error .");
    if &linkpath.is_some().to_string() == "/getnews" {
        let mut query = conn.query("SELECT * FROM news", ()).await.unwrap();

        let mut news_items: Vec<NewsItem> = Vec::new();
        while let Some(row) = query.next().await.unwrap() {
            let news_item = NewsItem {
                id: row.get(0).unwrap(),
                imgpath: row.get(1).unwrap(),
                name: row.get(2).unwrap(),
                description: row.get(3).unwrap(),
            };
            news_items.push(news_item);
        }
        return Ok(HttpResponse::Ok().json(news_items));
    } // Code go on .....
#

When i try this endpoint with get method
127.0.0.1:8000/api/get/getnews/
it returns 404

#

Idk maybe there is some stupid false

cosmic pewter
obsidian condor
#

no it's not, it's in the path

#

"/get/{path}/{id}"

cosmic pewter
#

Yes , i know okay so is there any method to create an optional path ?

#

For example i don't want create 2 different function for this

cosmic pewter
obsidian condor
#

nothing more than Path<(String, String)>

cosmic pewter
#

It isn't affect anything ?

#

Like create optional path ?

obsidian condor
#

no, it can't affect it

#

try ```rs
.service(web::resource("/get/{path}").to(get_data))

#

with the Options and maybe it will deserialize it correctly

#

you can have different paths to the same handler I think

cosmic pewter
#

Yep

#

Wait

#

I send

#
 if &linkpath.is_some().to_string() == "getnews" {
        let mut query = conn.query("SELECT * FROM news", ()).await.unwrap();

        let mut news_items: Vec<NewsItem> = Vec::new();
        while let Some(row) = query.next().await.unwrap() {
            let news_item = NewsItem {
                id: row.get(0).unwrap(),
                imgpath: row.get(1).unwrap(),
                name: row.get(2).unwrap(),
                description: row.get(3).unwrap(),
            };
            news_items.push(news_item);
        }
        return Ok(HttpResponse::Ok().json(news_items));
    } else if &linkpath.is_some().to_string() == "api/getlandingnews" {
        let mut query = conn
            .query(
                "select * from news where id = 1?",
                libsql::params![id.clone()],
            )
            .await
            .unwrap();

        let mut news_items: Vec<NewsItemLanding> = Vec::new();
        while let Some(row) = query.next().await.unwrap() {
            let news_item = NewsItemLanding {
                imgpath: row.get(0).unwrap(),
                name: row.get(1).unwrap(),
            };
            news_items.push(news_item);
        }
        return Ok(HttpResponse::Ok().json(news_items));
    }

Like this

#

Actually yes i know it isn't true , but just i want try something

obsidian condor
#

I don't know what you're showing

cosmic pewter
#

I want handle 5 different endpoints in 1 function

obsidian condor
#

why?

#

that's definitely not how you usually structure things

cosmic pewter
#

Just i've some free time and i want try something like this

#

What happens when i try something like this ( Some futuristic experiment )

obsidian condor
#

I still don't know what you're talking about

cosmic pewter
#

It's not my first time write a Back-End with Rust, it's just that a thought like this came to my mind all of a sudden and I wondered if it's possible to do something like this. If it's possible, how would it work, that's the kind of question I had, and I wanted to test it. Normally, I know that each function should have its own handler. It's just a simple code experiment I did to make good use of my free time. I'm not exactly sure what you didn't understand, but that's the gist of it

obsidian condor
#

at least none of the existing routers I know in any backend library support this use case

#

this only works as the last segment

#

so it wouldn't work with "/get/{path:.*}/{id}"

#

or maybe it would, idk

cosmic pewter
#

Just {tail} works

obsidian condor
#

works for what

cosmic pewter
#

For this
foo/bar/tail1
foo/bar/tail2 and etc.

cosmic pewter
obsidian condor
#

the name tail doesn't matter

cosmic pewter
#

What's difference ?

obsidian condor
#

I mean foo/bar/{tail:.*} matches foo/bar/one/2/three

cosmic pewter
#

Haaa i understand

#

Thanks