Http API call example via a plugin  Topic is solved

Discussions relating to Jiwa 7 plugin development, and the Jiwa 7 API.

Http API call example via a plugin

Postby Ernst » Mon Mar 10, 2025 9:05 am

I need to call a HTTP API with bearer authorisation using json data from a plugin, to send invoice information, to an External client.

Does anybody have an example plugin that includes an API call, Have tried but it just hangs.
User avatar
Ernst
Kohai
Kohai
 
Posts: 242
Joined: Tue Feb 19, 2008 3:43 pm
Topics Solved: 13

Re: Http API call example via a plugin  Topic is solved

Postby Mike.Sheen » Mon Mar 10, 2025 12:54 pm

Easiest way is to use the HTTPUtils extension methods.

Make sure you have imported the namespace ServiceStack.Text and you'll have access to the string extension methods - so in c# thats using ServiceStack.Text; and in VB it's Import ServiceStack.Text.

Example of POSTing a new debtor to our demo api at https://api.jiwa.com.au/Debtors using the bearer token 9vjoF4M5-gYVBWjoFyW9nlQieB9FhxpJkAsRJznB_Ok

If you must use a string json representation:
Code: Select all
// using a string as json
var response = "https://api.jiwa.com.au/Debtors".PostJsonToUrl("{ \"AccountNo\": \"Z123\", \"Name\": \"Mikes Test\"}", requestFilter: req => { req.Headers["Authorization"] = "Bearer 9vjoF4M5-gYVBWjoFyW9nlQieB9FhxpJkAsRJznB_Ok"; });


My preferred method is to use an anonymous object and have it serialised to json automatically for me - so I don't get the json formatting wrong, or have to worry about escaping quotes:
Code: Select all
// using an anonymous object
var secondresponse = "https://api.jiwa.com.au/Debtors".PostToUrl(new { AccountNo = "Z456", Name = "Mikes Test #2" }, requestFilter: req => { req.Headers["Authorization"] = "Bearer 9vjoF4M5-gYVBWjoFyW9nlQieB9FhxpJkAsRJznB_Ok"; });
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: 2583
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 807

Re: Http API call example via a plugin

Postby Ernst » Wed Mar 12, 2025 9:38 am

If I put that into C# I get
Capture1.PNG
Capture1.PNG (6.56 KiB) Viewed 12277 times


and similar message in VB

Have
Imports ServiceStack.Text
Dim response = "https://api.jiwa.com.au/Debtors".PostJsonToUrl("{ ""AccountNo"": ""Z123"", ""Name"": ""Mikes Test""}", requestFilter:=Function(req)
req.Headers("Authorization") = "Bearer 9vjoF4M5-gYVBWjoFyW9nlQieB9FhxpJkAsRJznB_Ok"
End Function)

Error:PostJsonToUrl is not a member of string.
What do I need to modify, Would HTTPclient work?
User avatar
Ernst
Kohai
Kohai
 
Posts: 242
Joined: Tue Feb 19, 2008 3:43 pm
Topics Solved: 13

Re: Http API call example via a plugin

Postby Mike.Sheen » Wed Mar 12, 2025 10:16 am

Ernst wrote:If I put that into C# I get
Capture1.PNG


and similar message in VB

Have
Imports ServiceStack.Text
Dim response = "https://api.jiwa.com.au/Debtors".PostJsonToUrl("{ ""AccountNo"": ""Z123"", ""Name"": ""Mikes Test""}", requestFilter:=Function(req)
req.Headers("Authorization") = "Bearer 9vjoF4M5-gYVBWjoFyW9nlQieB9FhxpJkAsRJznB_Ok"
End Function)

Error:PostJsonToUrl is not a member of string.
What do I need to modify, Would HTTPclient work?


Oh - my apologies! Iit's the ServiceStack namespace you need to import, not ServiceStack.Text!

So in c#:
Code: Select all
using ServiceStack;


You can also refer to the official ServiceStack HTTP Utils documentation for such issues.
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: 2583
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 807

Re: Http API call example via a plugin

Postby Ernst » Thu Mar 13, 2025 2:14 pm

THanks Mike, That worked great, managed to get it working via a plugin with a daily schedule to send invoices in JSON format nightly via API.

To this Purchase plus crowd. Looks popular in Aussie also, if any body there needs it.

http://www.purchaseplus.com

Level 5, 11 York Street, Sydney NSW Australia 2000 ,



If I wanted to add another header in the future e.g.
Accept: XX
ContentType:



How would I modify that Query.

Code: Select all
Dim response = "https://staging.api.purchaseplus.com/purchasing/api/invoices.json".PostJsonToUrl(TextOut, requestFilter:=Function(req)
 req.Headers("Authorization") = "Bearer XXX"
 End Function)
User avatar
Ernst
Kohai
Kohai
 
Posts: 242
Joined: Tue Feb 19, 2008 3:43 pm
Topics Solved: 13

Re: Http API call example via a plugin

Postby Mike.Sheen » Thu Mar 13, 2025 2:18 pm

Ernst wrote:If I wanted to add another header in the future e.g.
Accept: XX
ContentType:



How would I modify that Query.

Code: Select all
Dim response = "https://staging.api.purchaseplus.com/purchasing/api/invoices.json".PostJsonToUrl(TextOut, requestFilter:=Function(req)
 req.Headers("Authorization") = "Bearer XXX"
 End Function)


For the ContentType and Accept headers there are concrete properties you can set
So, try:

Code: Select all
Dim response = "https://staging.api.purchaseplus.com/purchasing/api/invoices.json".PostJsonToUrl(TextOut, requestFilter:=Function(req)
 req.Headers("Authorization") = "Bearer XXX"
 req.Accept = "application/json"
 req.ContentType = "text/html; charset=utf-8"
 End Function)


For other headers (as we did with Authorization) you just set it like req.Headers("MyHeader") = "MyValue"
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: 2583
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 807

Re: Http API call example via a plugin

Postby Ernst » Thu Mar 13, 2025 5:38 pm

OK thanks again..
User avatar
Ernst
Kohai
Kohai
 
Posts: 242
Joined: Tue Feb 19, 2008 3:43 pm
Topics Solved: 13


Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 4 guests