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
I would send the request using a class object, so I dont have to write long code
this is what Im actually doing
but in my rest API i had to do this for accept data from my C# application and postman or other programs/scripts
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?
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.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!
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
any example?
if I do like this its ok?
no clue. i don't use restsharp. haven't touched it in over a decade.
no i mean
its a good method? or not
it works with restsharp but I mean the method its ok or shitty
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.
I want perform a request like postman to my API
i would never use reflection like this.
i can do like this too
much better
since I know what properties the class has
i dont need dynamic things
im right no?
correct
the only problem is that description can be null :/
so I have to check if its null before do the request
if you want to add a null check before adding description to theh request, that's fine
my problem is that Im always insecure about myself
i want write good code but I never know if its good or not
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.
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?
I think you should always care to maintain best practices, even on private projects
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