posting a byte array

4,712 views
Skip to first unread message

mgroves

unread,
Mar 18, 2011, 1:45:37 PM3/18/11
to RestSharp
I have an object that I want to post to a REST endpoint (I'm using WCF
if that matters) in Json format. The object has a number of
properties, but the Json serializer seems to choke on the one byte[]
property of the object (it throws an exception saying that it can't
convert it to a string).

As a workaround, I'm UTF8 encoding the byte array, making the property
a string, and posting it that way. Is there a better way?

John Sheehan

unread,
Mar 18, 2011, 1:47:03 PM3/18/11
to rest...@googlegroups.com
what would a serialized byte array in json look like on the wire?

i'm going to guess that you've found the best way already :)

mgroves

unread,
Mar 18, 2011, 1:52:11 PM3/18/11
to RestSharp
I don't know what it *should* look like, but, as a UTF8 string it
looks like:

{
"data": "QmFzZSA2NCBTdHJlYW0="
}

For a byte[] array, WCF seems to expect something like this:

{
"data":
[81,109,70,122,90,83,65,50,78,67,66,84,100,72,74,108,89,87,48,61]

John Sheehan

unread,
Mar 18, 2011, 2:01:16 PM3/18/11
to rest...@googlegroups.com
This is via RestRequest.AddBody(object) right? For JSON, this just
passes through to JSON.NET, so we need to figure how to get that to
handle it.

mgroves

unread,
Mar 18, 2011, 9:45:36 PM3/18/11
to RestSharp
Yes, I was either using AddBody or request.AddParameter("application/
json",....)

I think we're just going to use Base64 encoding/decoding, but now I
have a different issue: we keep getting an error "Could not load file
or assembly 'Newtonsoft.Json, Version=3.5.0.0". We have
Newtonsoft.Json as a reference in the project, but it's version 4.0,
not 3.5. What am I doing wrong?

John Sheehan

unread,
Mar 18, 2011, 10:00:25 PM3/18/11
to rest...@googlegroups.com
That will be fixed in 100.4, coming out this weekend.

John Sheehan

unread,
Mar 18, 2011, 10:01:10 PM3/18/11
to rest...@googlegroups.com
Are you using NuGet?

mgroves

unread,
Mar 19, 2011, 9:53:08 PM3/19/11
to RestSharp
Yes, I'm using NuGet.

On Mar 18, 10:01 pm, John Sheehan <johnshee...@gmail.com> wrote:
> Are you using NuGet?
Reply all
Reply to author
Forward
0 new messages