Page 1 of 1

Http API call example via a plugin

PostPosted: Mon Mar 10, 2025 9:05 am
by Ernst
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.

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

PostPosted: Mon Mar 10, 2025 12:54 pm
by Mike.Sheen
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"; });

Re: Http API call example via a plugin

PostPosted: Wed Mar 12, 2025 9:38 am
by Ernst
If I put that into C# I get
Capture1.PNG
Capture1.PNG (6.56 KiB) Viewed 12279 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?

Re: Http API call example via a plugin

PostPosted: Wed Mar 12, 2025 10:16 am
by Mike.Sheen
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.

Re: Http API call example via a plugin

PostPosted: Thu Mar 13, 2025 2:14 pm
by Ernst
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)

Re: Http API call example via a plugin

PostPosted: Thu Mar 13, 2025 2:18 pm
by Mike.Sheen
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"

Re: Http API call example via a plugin

PostPosted: Thu Mar 13, 2025 5:38 pm
by Ernst
OK thanks again..