RestSharp request with image file and json body together

Hi! Im trying to send a POST request to my local API that accept files & json body. I need to perform a single request that sends the json data & the file.
26 Replies
ᴅᴊʟᴏʀ3xᴢo
ᴅᴊʟᴏʀ3xᴢoOP10mo ago
I would send the request using a class object, so I dont have to write long code
ᴅᴊʟᴏʀ3xᴢo
ᴅᴊʟᴏʀ3xᴢoOP10mo ago
this is what Im actually doing
No description
ᴅᴊʟᴏʀ3xᴢo
ᴅᴊʟᴏʀ3xᴢoOP10mo ago
but in my rest API i had to do this for accept data from my C# application and postman or other programs/scripts
No description
ᴅᴊʟᴏʀ3xᴢo
ᴅᴊʟᴏʀ3xᴢoOP10mo ago
if body contains data its from the C# application, but I want to use the body as it is without checking if data is present and I wont do long code any ideas?
The Fog from Human Resources
Why not encode the files bytes into base64 I added files directly to the body once using AddBody Like this request.AddBody(myFileBytes, myFileType); Where the file type was just smth like "image/jpg" Also I'd recommend working with JObjects when constructing your requests, anonymous types might cause issues later You're using Newtonsoft.Json anyways.
ᴅᴊʟᴏʀ3xᴢo
ᴅᴊʟᴏʀ3xᴢoOP10mo ago
binary its better eh I need to send a body with the file and the class data together i think im gonna do AddParameter for every class property since are only 3 properties so I dont have to write a lot of code if you have more ideas, tell me!
viceroypenguin
viceroypenguin10mo ago
the proper way to do this is using multi-part boundaries you could send one content part with the json data and theh other content part with the file data. I would recommend using HttpClient over RestSharp; RestSharp solves the problem that older bcl technologies didn't have convenient apis. HttpClient has much better apis, so you do not need to use RestSharp
ᴅᴊʟᴏʀ3xᴢo
ᴅᴊʟᴏʀ3xᴢoOP10mo ago
any example?
ᴅᴊʟᴏʀ3xᴢo
ᴅᴊʟᴏʀ3xᴢoOP10mo ago
if I do like this its ok?
No description
viceroypenguin
viceroypenguin10mo ago
no clue. i don't use restsharp. haven't touched it in over a decade.
ᴅᴊʟᴏʀ3xᴢo
ᴅᴊʟᴏʀ3xᴢoOP10mo ago
no i mean its a good method? or not it works with restsharp but I mean the method its ok or shitty
viceroypenguin
viceroypenguin10mo ago
depends on whaht you're trying to do. looks like you're doing form data, which is not whath you said you wanted to do originally.
ᴅᴊʟᴏʀ3xᴢo
ᴅᴊʟᴏʀ3xᴢoOP10mo ago
I want perform a request like postman to my API
viceroypenguin
viceroypenguin10mo ago
i would never use reflection like this.
ᴅᴊʟᴏʀ3xᴢo
ᴅᴊʟᴏʀ3xᴢoOP10mo ago
i can do like this too
No description
viceroypenguin
viceroypenguin10mo ago
much better
ᴅᴊʟᴏʀ3xᴢo
ᴅᴊʟᴏʀ3xᴢoOP10mo ago
since I know what properties the class has i dont need dynamic things im right no?
viceroypenguin
viceroypenguin10mo ago
correct
ᴅᴊʟᴏʀ3xᴢo
ᴅᴊʟᴏʀ3xᴢoOP10mo ago
the only problem is that description can be null :/ so I have to check if its null before do the request
viceroypenguin
viceroypenguin10mo ago
if you want to add a null check before adding description to theh request, that's fine
ᴅᴊʟᴏʀ3xᴢo
ᴅᴊʟᴏʀ3xᴢoOP10mo ago
my problem is that Im always insecure about myself i want write good code but I never know if its good or not
viceroypenguin
viceroypenguin10mo ago
first, you're going to write lots of crappy code before you write good code. that's ok. you're learning. second, you're on a decent track; you're asking good questions, and you're trying things out. third, i would recommend you stop learning restsharp - it's not used often these days, so it likely won't help you when you encounter real code.
ᴅᴊʟᴏʀ3xᴢo
ᴅᴊʟᴏʀ3xᴢoOP10mo ago
mh okey thanks a lot ❤️ ok nothing for some reasons the API isnt getting description, so I'll leave as I did last night as I did here its a private code so.. doesnt care too much but im really sad because be a beginner (on API things) its very bad :( i use a lot C# but never did these things regarding API stuff with files upload what u think @viceroypenguin | 🦋🐧? (sorry for ping) i mean, should I care of a private code?
The Fog from Human Resources
I think you should always care to maintain best practices, even on private projects
ᴅᴊʟᴏʀ3xᴢo
ᴅᴊʟᴏʀ3xᴢoOP10mo ago
ah i know but there are no good methods for do it or I do as I did at the start of this conversation or i use 2 endpoints, one for upload and one for data idk im so confused
Want results from more Discord servers?
Add your server