I'm trying to post data to a URL and get byte array back using ServiceStack.
byte[] pdf = await "https://some-url.com/that-expects-post/and-returns/pdf-file"
.PostToUrlAsync(new
{
param1 = "aaa",
param2 = 123
},
requestFilter: req =>
req.Headers.Add("Cookie",
cookies.Select(x => $"{x.Name}={x.Value}").Join(";")));
the request works and I get a PDF file back, but, in string format, not bytes. I tryed to save this string into file, but this didn't work.
How can I solve this?
Have a look at the HTTP Utils implementation in HttpUtils.HttpClient.cs to see how the different extension methods are implemented where they're just convenience wrappers around SendStringToUrl/Async
and SendBytesToUrl/Async
.
To receive bytes you'll need to one of the *Bytes
convenience methods like PostBytesToUrlAsync
, but they only accept a bytes Request Body which you'll need to construct yourself.
E.g. you can send the same Form Data request that PostToUrl
APIs send with:
var formData = QueryStringSerializer.SerializeToString(new {
param1 = "aaa",
param2 = 123
}).ToUtf8Bytes();
var pdf = await url.PostBytesToUrlAsync(formData,
contentType: MimeTypes.FormUrlEncoded,
requestFilter: req => req.Headers.Add("Cookie",
cookies.Select(x => $"{x.Name}={x.Value}").Join(";")));