Wednesday 22 April 2015

Consuming an HTTP/JSON REST service with webMethods

Type: How-to and step-by-step tutorial
Tags: REST, HTTP, JSON
Product: webMethods Integration Server. Tried it with version 9.6

When to apply: You've got a flow service that needs to consume a REST service - whether it's RESTful or "not so RESTful".
This document explains how to consume a REST call by converting an Integration Server document to JSON and sending it by making an HTTP call.

Example repository: https://github.com/juanaltech/webmethodsRestConsumerExample

Summary of steps

Prerequisistes

Create a document type that defines the structure of the JSON content.

Steps

In the flow service that calls the REST service, add the following:
  1. Populate the content of document of the type created in the initial step
  2. Use the pub.json:documentToJSONString built-in service to convert the document to a JSON string
  3. Use pub.client:http to make an HTTP request. The json string from the previous step needs to be mapped to data/string. You'll also need to set values for fields such as url and method. Finally, add the necessary headers for the request. You'd normally need to add:
    • Content-Type, with a value of the resource type for the request and, optionally, the encoding. This may be application/json; charset=UTF-8 in simple cases.
    • Accept, with a value of the expected resource type of the response. This may be application/json in simple cases.

Step-by-step Example

In this simple example, I'll show you how to consume a hypothetical REST service in order to send information about a car.

You may grab the code from: https://github.com/juanaltech/webmethodsRestConsumerExample

Create package and folder

You may use use an existing package with a folder hierarchy. In our example, we'll create a new package package and a restconsumer folder. First, select New Package, type in the the package name, RestConsumerDemo, and select OK. Next, right click on the containing package and select New Folder and create a new folder called restconsumer.

Create a document type

Right click on the restconsumer folder and select New Document Type. We'll assign it a name of Car

In the document type pane, right click on a blank space and select insert String to create a field named make. Repeat this step to create a field named model.

Create the flow service


Right click on the restconsumer folder and select New Flow Service. Type in consume as the name and hit OK.

Right click on an empty spot in the Tree tab, select Insert, Map. 


Make sure the pipeline view is visible and then drag the Car document type from the Package Navigator into the Pipeline Out section in the pipeline view. This will create a new document of that type. Rename the document's name to car.

In order to assign values to make and model, right click on the field and select "Set Value". We'll use "Volkswagen" and "Golf".

Add pub.json:documentToString service invocation

In order to add a new step in the service, right click on an empty spot in the Tree tab again and select Insert from the context menu and select Invoke. Navigate to the WmPublic package, select the pub/json folder and then select the documentToString service.

Map the car document in pipeline in to the document field in documentToJsonString by dragging one field onto the other - once you do that you'll see a black line connecting both fields. Notice a jsonString value is created automatically in Pipeline Out.


Add pub.client:http invocation

Right click on an empty space in the Tree tab again. This time insert an invocation to pub.client:http from the public WmPublic package.

Now all the action happens in the the pipeline view. Assign all the values that are required. In this example, we'll assign the following:
  • url: specify the URL to your resource. We'll assume the resource is available at http://localhost:8089/cars
  • method: we'll use put in our example
  • Map the jsonString field to the data/string field in pub.client:http
  • Right click on the headersService In field and to add headers with a type of String. We'll add two:
    • Content-Type, which will have a value assigned of application/json; charset=UTF-8
    • Accept, with a value of application/json

Try it

You may now run the consume service by right clicking on it, selecting Run As and Run Flow Service. If you capture the request, you'll notice a string like this is sent in the body: {"make":"Volkswagen","model":"Golf"}

If you get a connection refused error, it's most likely because there's no service provider on the URL you specified. Once you make sure it's pointing to the right location, you should see a value of 200 on the response status code inside the results pane.



Recommendations

In order to provide robustness, a complete implementation should typically check the header's status (HTTP status code) to ensure the returned value falls in the range between 200 and 299. 

When  other values are returned, e.g., 500, your code should typically exit with a failure or throw an  exception.


31 comments:

  1. Excellent tutorial and very, very useful.
    as an extra, you could add how to read a json response.
    1. pub.string.bytesToString of Body/bytes
    2. pub.json.jsonStringToDocument

    ReplyDelete
    Replies
    1. Hi Federico,

      Do you mean the two services which are mentioned for inbound JSON data?

      Delete
  2. I am working with 8.2 and can't upgrade to 9.6..I have a requirement to expose json end point to clitents. how do I do it?

    ReplyDelete
    Replies
    1. Hi n,

      At a fist look it seems that all that Juanal did with 9.8 can be done also with 8.2.
      Except for the services documentToJSON and jsonToDocument. This services are not available in 8.2.
      For the documentToJSON however, you can easily create a recursive JAVA service that transforms the IData into an JSON string.

      Delete
  3. Hi ,

    I am facing issue wherein when I send data for DELETE operation, it does not go properly.

    ReplyDelete
  4. Hi n and Impailer

    For those using an older webMethods, such as 8.2, an option is to use the Tundra library (https://github.com/Permafrost/Tundra). It provides the tundra.json:emit service (https://github.com/Permafrost/Tundra/blob/master/SERVICES.md#tundrajsonemit), which generates a JSON representation of an IS document.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Thanks Juanal, its very much useful.

    ReplyDelete
  7. Hello..
    Please, how Can I make a Json string with numeric fields, however withou "" character.
    Something like this:

    {"Name":"Ronaldo","Age":42,"Gender":"M"}

    Take a look into Age field value, that is without "".

    Thank you.

    Ronaldo

    ReplyDelete
  8. Hello..
    Please, how Can I make a Json string with numeric fields, however withou "" character.
    Something like this:

    {"Name":"Ronaldo","Age":42,"Gender":"M"}

    Take a look into Age field value, that is without "".

    Thank you.

    Ronaldo

    ReplyDelete
  9. Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updatingmulesoft online training

    ReplyDelete
  10. I am really very happy to find this particular site. I just wanted to say thank you for this huge read!! I absolutely enjoying every petite bit of it and I have you bookmarked to test out new substance you post.
    python interview questions and answers
    python tutorials
    python course institute in electronic city

    ReplyDelete
  11. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information. 
    Best Devops Training in pune
    excel advanced excel training in bangalore

    ReplyDelete
  12. Thank you for an additional great post. Exactly where else could anybody get that kind of facts in this kind of a ideal way of writing? I have a presentation next week, and I’m around the appear for this kind of data.
    angularjs Training in chennai

    angularjs Training in chennai

    angularjs-Training in tambaram

    angularjs-Training in sholinganallur

    angularjs-Training in velachery

    angularjs-Training in pune

    ReplyDelete

  13. Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.

    Advanced AWS Training in Marathahalli |No.1 AWS Training in Marathahalli
    Best AWS Amazon Web Services Training Institute in Chennai | No.1 AWS Training Institutes for Solution Architect in Chennai | Advanced AWS Certification Training in Chennai

    ReplyDelete
  14. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
    rpa training in bangalore
    best rpa training in bangalore
    rpa training in pune

    ReplyDelete
  15. What will be the published url of REST service created above? I tried 2,3 urls but unable to call it from Postman. My Service is working on designer, i need published url so that some other system can call webmethods integration service.

    Kindly support

    ReplyDelete
  16. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on sharing!!.

    android training in chennai

    android online training in chennai

    android training in bangalore

    android training in hyderabad

    android Training in coimbatore

    android training

    android online training

    ReplyDelete
  17. I am really very happy to find this particular site. I just wanted to say thank you for this huge read!! I absolutely enjoying every petite bit of it and I have you bookmarked to test out new substance you post.
    web designing training in chennai

    web designing training in omr

    digital marketing training in chennai

    digital marketing training in omr

    rpa training in chennai

    rpa training in omr

    tally training in chennai

    tally training in omr

    ReplyDelete
  18. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site
    Webmethods Training in Bangalore

    ReplyDelete
  19. Lakers Portland Odds (2021) - Casinoland 메리트카지노 메리트카지노 jeetwin jeetwin 66Golden Casino Review 2021 - Bonuses & Bonuses

    ReplyDelete
  20. titanium dog teeth implants and a new replacement for
    The brand new titanium dog teeth pad allows for the titanium spork removal stiletto titanium hammer of a razor blade from a razor blade. This is t fal titanium pan the best titanium flat iron only way you babylisspro nano titanium can do your nails.

    ReplyDelete
  21. Great post. keep sharing such a worthy information.
    Web Designing Course In Chennai

    ReplyDelete
  22. Sports Betting - Mapyro
    Bet the moneyline from 출장안마 1:25 PM to 11:00 PM. See more. 출장안마 MapYO Sportsbook features live odds, deccasino live streaming, 메이피로출장마사지 and detailed information. https://febcasino.com/review/merit-casino/

    ReplyDelete