1

I'm consuming a rest service with ServiceSatck framework. Now I want to get the raw bytes. Suppose the url is http://xx.xxx.xxx.xx/Myservice/api/hello.

In this Seeing the Http Response content in ServiceStack it seems to be using get method but I used post method.

EDIT:

I used the code

`var x = "http://xx.xxx.xxx.xx/Myservice/api/hello".PostJsonToUrl(new MyRequestDTO() { RequestData = hello }).ToArray();

I did get the raw bytes. However comparing with RestSharp method, there are about 200 bytes lost.

Using RestSharp method, the code is:

        var aResponse = restClient.Execute(MyRequestDTO);
        byte[] bytes = aResponse.RawBytes;
Community
  • 1
  • 1

1 Answers1

1

The documentation for ServiceStack's typed .NET clients shows how you can access the raw bytes:

byte[] responseBytes = client.Get<byte[]>("/poco/World");
var dto = responseBytes.FromUtf8Bytes().FromJson<PocoResponse>();
dto.Result //Hello, World

Or as a Stream:

using (Stream responseStream = client.Get<Stream>("/poco/World")) {
  var dto = responseStream.ReadFully().FromUtf8Bytes().FromJson<PocoResponse>();
  dto.Result //Hello, World
}

You don't even need to use the type clients for getting raw bytes, you can just as easily use a HTTP Util extension method:

byte[] rawBytes = "http://example.org/Myservice/api/hello".GetBytesFromUrl();

HOW to POST JSON and retrieve bytes using HTTP Utils extensions

var dtoBytes = new MyRequestDTO { ... }.ToJson().ToUtf8Bytes();
var responseBytes = "http://example.org/Myservice/api/hello".PostBytesToUrl(
  dtoBytes, contentType:"application/json");
mythz
  • 141,670
  • 29
  • 246
  • 390
  • I want to use the third one since the first one is not working. What is argument for GetBytesFromUrl? Suppose I have a request. Is it "http://example.org/Myservice/api/hello".GetBytesFromUrl("*/*", new MyRequest() {RequestData = MyDTO})? –  Jun 20 '13 at 20:33
  • No, there's no Request DTO. HTTP Utils is for calling any 3rd Party HTTP Urls it doesn't use Request DTOs. Just specify what you want on the Url and read the docs for more info. – mythz Jun 20 '13 at 20:41
  • I want to post a request(a dto object) to the api and get the raw bytes from the response, but the first one's argument(client.Get –  Jun 20 '13 at 20:45
  • @Love Don't use comments to ask new questions, be specific on what answer you're seeking and include it in the original question. Also read the docs, it feels like you haven't bothered. – mythz Jun 20 '13 at 20:59
  • Please see my updated code, I do have a request dto to be posted to the server. –  Jun 21 '13 at 13:13
  • @Love Why would you think `PostJsonToUrl` returns bytes when it returns a string? The most obvious API to use is `PostBytesToUrl` to POST a HTTP Request which returns bytes. But honestly if you have it working with RestSharp just use that, it doesn't seem that you're understanding what the HTTP Utils extension methods do. – mythz Jun 21 '13 at 13:30
  • Because if you use PostBytesToUrl, you have to convert the argument from dto object to byte[]. I am not sure how to do it. Of course, using RestSharp is fine. I just want to learn something about ServiceStack. –  Jun 21 '13 at 13:36
  • HTTP Utils is for any HTTP Service it doesn't have any opinion tying it to only ServiceStack services. I've updated the answer to show how to use `PostBytesToUrl`. – mythz Jun 21 '13 at 13:46
  • I will give up this method and stick on RestSharp. It(PostBytesToUrl) returned 0 bytes. But many thanks for your input. –  Jun 21 '13 at 13:55