Page 1 of 1

httputils example

PostPosted: Thu May 06, 2021 9:41 am
by SBarnes
Is there a working example anywhere of using httputils with an apikey and a post that returns data in the body of the call, test code form Jiwa's test suite if it exists would suffice?

Re: httputils example

PostPosted: Thu May 06, 2021 12:53 pm
by Mike.Sheen
SBarnes wrote:Is there a working example anywhere of using httputils with an apikey and a post that returns data in the body of the call, test code form Jiwa's test suite if it exists would suffice?


We use the httputils extensions to post webhooks - with any request headers if they were specified - in the REST API plugin there is this block of code that does it:

Code: Select all
// This is the actual POST to the webhook
subscription.URL.PostStringToUrl(message.Body,
                         requestFilter: webRequest =>
                         {
                            foreach (SY_WebhookSubscriptionRequestHeader subscriptionRequestHeader in subscriptionRequestHeaders)
                            {
                               webRequest.Headers[subscriptionRequestHeader.Name] = subscriptionRequestHeader.Value;
                            }
                         });

Re: httputils example

PostPosted: Thu May 06, 2021 1:05 pm
by SBarnes
Thanks Mike,

I am aware of that bit from the REST api I was more after how you get the response's content. I found AddBearerToken as an extension method that I think will take care of the api key but I am unsure how the response works by the look of what I found I think you ned to do something like this but was wondering if there is something simpler that doesn't require the stream to get at it?
Code: Select all
 responseFilter: webResponse =>
{

             System.IO.Stream receiveStream = webResponse.GetResponseStream();
             System.IO.StreamReader readStream = new System.IO.StreamReader( receiveStream);
             string response = readStream.ReadToEnd();
}


Re: httputils example

PostPosted: Thu May 06, 2021 1:09 pm
by Mike.Sheen
SBarnes wrote:Thanks Mike,

I am aware of that bit from the REST api I was more after how you get the response's content. I found AddBearerToken as an extension method that I think will take care of the api key but I am unsure how the response works by the look of what I found I think you ned to do something like this but was wondering if there is something simpler that doesn't require the stream to get at it?
Code: Select all
 responseFilter: webResponse =>
{

             System.IO.Stream receiveStream = webResponse.GetResponseStream();
             System.IO.StreamReader readStream = new System.IO.StreamReader( receiveStream);
             string response = readStream.ReadToEnd();
}



Ahh - that I don't know. I do know you can get the response from when using the ServiceStack client by passing in a response filter - maybe something similar is possible with httputils (and not using a stream).

Re: httputils example

PostPosted: Thu May 06, 2021 1:31 pm
by SBarnes
The bit of code I gave was off httputils so you can do it with a response filter same as with the client but according to https://docs.microsoft.com/en-us/dotnet ... mework-4.8 the only methods on HttpWebResponse are GetResponseHeader, GetReponseStream and GetObjectData so I think the only choice unfortunately is the stream code or my own extension method to encapsulate it if needed in multiple places.