this post was submitted on 12 Jul 2023
8 points (90.0% liked)

Lemmy

12572 readers
1 users here now

Everything about Lemmy; bugs, gripes, praises, and advocacy.

For discussion about the lemmy.ml instance, go to !meta@lemmy.ml.

founded 4 years ago
MODERATORS
 

I'm toying about with writing a client in Rust.

Example code online uses the API endpoint https://lemmy.ml/api/v3/post/list, and when I go to this link in a browser, it doe indeed load data. However, when I try to load it from a program, it gives me a 403 Forbidden. Does anyone know why this could be?

(code)

top 12 comments
sorted by: hot top controversial new old
[–] RoundSparrow@lemmy.ml 2 points 1 year ago (1 children)
[–] SubArcticTundra@lemmy.ml 1 points 1 year ago* (last edited 1 year ago)

Didnt know about this, thanks

[–] CMahaff@lemmy.world 1 points 1 year ago* (last edited 1 year ago) (1 children)

This one got me too.

lemmy.ml specifically is running requests through a user agent checker - and if it's empty, which reqwest is by default, you will get a 403.

You can see an example here of using ClientBuilder to set a user agent string: https://github.com/CMahaff/lasim/blob/main/src/lemmy/api.rs

So set any string (should be your project name) and then it should work.

[–] SubArcticTundra@lemmy.ml 1 points 1 year ago

Oh great thank you

[–] nmtake@lemm.ee 0 points 1 year ago (1 children)

Can you run the code against another instance, and curl https://lemmy.ml/api/v3/post/list?

[–] SubArcticTundra@lemmy.ml 1 points 1 year ago

Strange, you're right, it does seem to work when I run it on lemmy.world instead

[–] marsara9@lemmy.world 0 points 1 year ago (1 children)

I don't see anything wrong initially. Can you also test with something like postman? It'll allow you to control the headers etc.. that you send.

You can also compare to what I have here and see if that helps: https://github.com/marsara9/lemmy-search/blob/b6c88355aba49abca52862473650526821ee165a/server/src/api/lemmy/fetcher.rs#L123

[–] SubArcticTundra@lemmy.ml 0 points 1 year ago (1 children)

Ah I'll try that, thanks. This is unrelated, but is it normal for my project folder for this to have 3GB, given that these are my dependencies?:

lemmy_api_common = "0.18.2"
reqwest = "0.11.18"
tokio = "1.29.1"

I'm quite new to rust and I realize that some languages don;t share dependencies between projects

[–] marsara9@lemmy.world 0 points 1 year ago

The target folder may be quite large. You can look at the dependencies for my project but my end binary is only a few MB.

[–] lemann@lemmy.one 0 points 1 year ago (1 children)

Not familiar with Lemmy's API specifically but you might need to be logged in for that...

Do you get a 403 when using a private browser window, logged out of lemmy?

[–] SubArcticTundra@lemmy.ml 0 points 1 year ago* (last edited 1 year ago)

Oh, didn't think of this. True I did make the request from the browser that I use to browse Lemmy. Edit: Nope, wtill loads in Incognito... 🤔

[–] Saganaki@lemmy.one -1 points 1 year ago

I know very little about Lemmy specifically, but 403 generally means you’re not auth’d or don’t have permission.

Do you need to set an auth header perhaps? Your best bet would be to bring up browser dev tools and see what request the working browser is doing.

load more comments
view more: next ›