Click Ahead
Click Ahead

Reputation: 2852

Is is possible to Post a file and json body with RestSharp?

I'd like to post one file and a json object to my server. Posting as a Parameter works fine, so this works:

request.AddFile ("MyPic", organisation.Pic, "Pic.png", "image/png");
request.AddParameter ("name", "test name");

But if my JSON object is something with a nested structure like:

{organisation:{
   'name':'test',
   'address':{
     'line1':'foo',
     'line2':'foo2'
   }
 }}

How do I post this, while maintaining the json structure.

If I set it in the content body i.e.:

request.AddBody (organisation);

the content is posted, but the file is not posted.

Is it possible to post both a json body and file?

Upvotes: 1

Views: 1826

Answers (3)

Michael
Michael

Reputation: 168

Actually this will be possible very soon. There's a pull request on the RestSharp library that addresses this. I've started maintaining the project and will be merging it into master in the next day or so. Once there's enough stuff, I'll release a new NuGet package as well.

Upvotes: 0

Click Ahead
Click Ahead

Reputation: 2852

So, I figured out a workaround, which suits my purposes. What I was looking for was to do a single post of both the file and associated meta-data. In this instance I serialised the object to a JSON string, set that as my parameter and de-serialised on the server. This works in my case but I accept that this may not be ideal for others. Thought it might help someone.

Upvotes: 2

AntonioOtero
AntonioOtero

Reputation: 1789

No. When you use AddFile you are actually addding a body to your request. You can't send a request with 2 bodies.

Upvotes: 1

Related Questions