Requests
Hello, i have never done something that i will say before, how can i send a request to an API Url, get responce in a form of a .json file and then getting specific values from the .json into variables. How can i do that?
77 Replies
HttpClient
Then .GetFromJsonAsync<T>()
Where T
is a class that represents the JSON responseIs the API returning a json file or a json response?
Json File you'll need to handle slightly different as it's a text file which has a small bit of headers and you need to open a stream reader, I think? I want to say a text file cant be read exactly the same way as a raw byte stream of text, the file has a little bit of stuff at the start to go "Im a file!"
json responce
oh then thats easy yeh, what Z23Z said is 👍
how could i pass data into httpclient
What data?
A GET request has no body, only query parameters
for exaplme the api im using requires username + password to be passed in order to get a valid login token
Ah, a POST request then
.PostAsJsonAsync<T>(url, data)
is it FormContent or Body or Url Params?
"Content-Type: application/json"
body
Body, aight
So something like this?
FormUrlEncodedContent
is for when it has to be submitted as FormContent
PostAsJsonAsync
is when it has to be submitted as just application/json
in the bodyhttpclient.PostAsJsonAsync?
ah just needed to add a reference to http.formating
altho im confused on what type the "value" has to be
SomeClass
being a class that describes the data you get from the endpointokay thanks
also if you dont mind how can i enable cs9+ , i cant find <LangVersion> inside my .csproj
What's the framework version you're using?
i find that in .csproj?
Yep
4.8
Oof
Why not use a recent version of .NET instead?
idk xD i never got to update it
+idk how to do it
You probably created a project with .NET Framework instead of .NET
For reference, $newproject
When creating a new project, prefer using .NET over .NET Framework, unless you have a very specific reason to be using .NET Framework.
.NET Framework is now legacy code and only get security fix updates, it no longer gets new features and is not recommended.
https://cdn.discordapp.com/attachments/569261465463160900/899381236617855016/unknown.png
never touched those things
Did you not create this project, then?
aight lemme remake my project
i did but i used .net framework
Well, shouldn't have
dw i didnt go much into it
aigh so i got 7.0 rn
8.0 is current
Might have to update VS to use it, tho
hm gimme a sec
i updated it, we will talk tmr if thats fine i have to do smt rn
I wish "paste json as class" automated this, hopefully eventually this becomes a thing
@ZZZZZZZZZZZZZZZZZZZZZZZZZ ive got the framework on my project how do i enable cs 9 now?
If you have a .NET 8 project, you should be on C# 12 from the get-go
ah okay tyty
ive never met this error before:
i mean i did but i do not understand it
You need the extension method from... uh, whatever namespace
See if quick fixes fix it
ah okay
System.Net.Http.Json
is where thos extension methods arenow it doesnt show up in COM
oh okay
there was http.extentions one
installed it and included it still doesnt fix it
Wym "installed"?
It's not a nuget, no need to install anything
it is
its not in the COM list
for me
installing this fixed it
Huh, maybe it is, then
before i installed the latest vs i didnt have to install it, cus i installed a package requireing it before which prob installed it
@ZZZZZZZZZZZZZZZZZZZZZZZZZ sorry for the ping, i stopped working on this for some time as of irl stuff, how do i get the data from the class inputed into ReadFromJsonAsync<>();?
The example you mentioned is how
There isn't anything more to it
im confused lmfao
what example did i mention
My message you replied to
i got my "responeData"
i got that
but how do i get the value that has been returned from the response
From
responseData
like my class has a int Token, how do i get that Token from responseData
The
SomeClass
class should describe the data you expect to receiveyeah ive got that
So if you expect to receive
you will have a class
and after getting the data from the stream
you can get the status with
yea but how do i get the value of "Count" for example from responseData
responseData.Count
i did that with my "Token" variable
but it wasnt under responsedata
this might be easier to understand
tyty, im getting status 443 from the api url, it means that the url endpoint doesnt exist?
I don't remember every status code by heart
Look up what it means on MDN
Notice your lack of
await
im blind
sorry fr this time
late night coding 10/10 would not recommend
im confused, out of nowhere it throws an "System.Text.Json.JsonException: ''I' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.'" from System.Text.Json.JsonException, i didnt touch that part of my code, google search did not help
What's the actual response you get from the API?
Run it via some REST client like Bruno or Nightingale
Or even just open it in the browser
Chances are, that for some reason what you get from that URL isn't valid JSON
how can i pass the json parms trought tho
Right, that's why I mentioned using some client
You can pass params there at will
Alternatively, read the response as a string and log it to the console
ill extract the response into a plain text file
That'll do in a pinch as well
Yeah
i get returned "Internal Server Error" as plain string text, an issue of the api was already created on github but closed ill make anothjer one
Ah, that'll do it, yeah
Might want to check the response status first, before trying to deserialize it
i think its 505
That way you can actually tell that an error has occured instead of crashing the app
nvm
i have trycatch dw
Yeah ill try tmr some poeple were saying they get same issue around same time.