httputils example

Discussions relating to the REST API of Jiwa 7.

httputils example

Postby SBarnes » Thu May 06, 2021 9:41 am

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?
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: httputils example

Postby Mike.Sheen » Thu May 06, 2021 12:53 pm

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;
                            }
                         });
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: httputils example

Postby SBarnes » Thu May 06, 2021 1:05 pm

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();
}

Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: httputils example

Postby Mike.Sheen » Thu May 06, 2021 1:09 pm

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).
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: httputils example

Postby SBarnes » Thu May 06, 2021 1:31 pm

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.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175


Return to REST API

Who is online

Users browsing this forum: No registered users and 1 guest

cron