65 Replies
probably because your passing invalid json
Hmm...
Get it working via something like PostMan first
put a breakpoint and validate json contains what you think it should
What's postman?
""content"": system
guaranteed to be brokenI see
It's a tool for making web requests
you are not actually adding the user/system to the body
you are hardcoding in the word
user
and system
without quotesfelt like it
How do I put variables into json?
interpolated string
I know it works on the server side
One way is to create a C# object that you then serialize to the JSON rather than hand-building the string
Seems easier than what I just did lol
I still usually prefer mtreits suggestion of setting up actual types that reflect your structure and letting the system handle the json encoding
hmm
but this is fine, if verbose
thanks!
Hmm.. It still seem to not work...
I can do it via curl
but not in c#?
woops, no quotes around the values
hmm
updated
I see
well I actually didn't use the values but I still can't get it to work properly?
set a breakpoint, verify that the body looks okay
I'm not sure what a breakpoint is
$debug
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
time to learn 🙂
breakpoint at the variable?
after the json variable is set, ye
This is why I suggest using Postman. That verifies you are actually building a request the server accepts. Then you can compare what Postman sends over the wire with what your C# code does
It says that json is null?
should there be two dollar signs?
okay I think I let it execute
smart..
this is the value of json
should there be \r?
\r
is carriage return
\r\n
is a windows style newlineI see
What should I look out for?
or just do proper classes to serialize instead of fighting text to fit
if the start and end quotes are there, thats probably why
also uh
this is super invalid
I see
thats what you are sending
hmm
probably incorrect yes
wdym probably lol
nothing lol
json validity is a boolean state 😄
I didn't know you could turn classes into json?
thats how you normally do it
😔 guess they need to invent those quantum computers soon so I can have a probably valid json
hmm
then just serialize chat instead 🙂
uhuh...
so it would literally just be
or use JsonContent
with System.Text.Json
or
PostAsJsonAsync
yep that works too
and sorry to interrupt mid journey 🙂
😂 it's no mid journey
looks more complicated
but it works
looks good then
unlike your 10+ attempts at doing it yourself 🙂
manually writing json is a mess
he just made a onliner out of my classes into a record
avoid when possible
in terms of how different it is
but yes create DTOs to represent your json it will save your life
this is the "shortest" way to do it properly, but it also means you will get chunked transfer encoding
if the webserver doesnt support that, there are workarounds.
as I found out the hard way at work a few days ago 😄
well I want to use leowest's example but the class is not a string nor httpcontent?
wait-
I forgot the other part
you have to create the objets then serialize it or use the method pobi said above
PostAsJsonAsync
with what u have it would be something like this
with what pobi suggested it would be just
you choose whichever u feel more confortable with
and you can mix and match
you could use the classes, but still use PostAsJson
I would normally add a helper method with records like thesee
the method itself becomes very clean and easy to use then
the records you can "hide" in some other file somewhere 😄