Ronan Keane
Ronan Keane

Reputation: 189

Moqui - send a RESTful sendJSONRequest() to remote URL?

Moqui - send a RESTful sendJSONRequest() to remote URL?

How should I send a JSON Request to a remote URL with Moqui?

Are there in-built methods existing in Moqui, or what do I need to do? Do I need to work directly with the JsonBuilder and JsonSlurper groovy classes, simpleHttpStringRequest() method and direct CURL statements to write a script? (I am a beginner in Java and Groovy.)

E.g. Service needed:

<service verb="create" noun="RcSupplier">
 <!-- 1. Parse my service's in-parameters and the corresponding remote application parameter field names to needed JSON request body. Also include correct header, authorisation, method, etc. 
 <!-- 2. POST JSON to remote application https://api.xxxxxxxxxxxx.com/api/v1/Suppliers ("Create" Supplier) -->
 <!-- 3. Check response is ok and handle errors --> 
 <!-- 4. GET the Supplier ("find" Supplier) just created on the remote app, to retrieve the primary key (id) that was generated for it by the remote system (either append $filter=code eq 'partyId' or else retrieve all Suppliers as a list to perform our own query) -->
 <!-- 5. Parse the JSON response body received to a map that will be utilised to update my corresponding entity (field: externalId) with the remote primary key value. (note: probably better to use EntityDataLoader to do this later on for check and other functionality?) -->
</service>

Upvotes: 0

Views: 332

Answers (2)

David E. Jones
David E. Jones

Reputation: 1776

There is no need for JSON generation or HTTP client code in Moqui Framework, there are good tools available for that. My preferred tool for JSON is the classes in Groovy (JsonBuilder or JsonOutput), see:

http://docs.groovy-lang.org/latest/html/gapi/groovy/json/package-summary.html

For the HTTP request you can use the StupidWebUtilities.simpleHttpStringRequest() method, but it is just a thin wrapper around the Apache HTTP Client library for a simple, common case. You can use the Apache HTTP Client library directly too.

Upvotes: 1

Sam Hamilton
Sam Hamilton

Reputation: 131

Have a look at the examples in the Example folder and also checkout the REST examples here too but probably most helpful for you is the existing application for Authorize.net, where there is an working example of a remote service call using XML so switching to JSON should not be too difficult

Upvotes: 0

Related Questions