soapUI How to Post JSON to a REST Service

Automation Testing Published on:

POST JSON to a REST service using soapUI

Since my previous post on How to Test a REST Web Service I've received a few requests asking for an example using JSON. So as requested here is a quick example on how POST JSON to a REST service using soapUI.



Setup – CouchDB

To make this example as easy as possible, without having to setup any sort of authentication, I've decided to use Apache CouchDB. CouchDB is easy to install, stores data as JSON documents and has an easy-to-use API to interact with it. This makes it a great application to use to teach us more about REST testing with soapUI and JSON.

To install couchDB go to:

  1. http://couchdb.apache.org/
  2. Click on Download and choose the version you want to us
  3. Once it's installed, enter http://127.0.0.1:5984/_utils/ in a browser. This should bring up your local CouchDB Futton web site.
  4. Next, click on the Create Database and name it “soapUI”
  5. Navigate back to http://127.0.0.1:5984/_utils/index.html. Notice that soapui shows 0 docs.

How to use soapUI to send JSON to a REST API

Here are the steps to follow to get started using the free version of soapUI with REST testing:

  1. Create a new project in soapUI. Enter Project Name: JSON. Click OK

  2. Right Click on the JSON project we just created and select New REST Service
  3. In the New REST Service screen enter:

    Service Name: JSON

    Service Endpoint: http://127.0.0.1:5984/

    Check ‘Opens dialog to create a REST Resource'


  4. In the New REST Resource enter:

    Resource Name: soapui

    Resource Pat/Endpoint: soapui

    Click OK


  5. In the New REST Method screen enter:

    Method Name: PostJSONName

    HTTP Method: POST

    Click OK


  6. This is where most people get confused. Notice under the Request 1's Media Type how there is not an option for JSON


Not worries—all you need to do is type application/json into the Media Type field

  1. In the text area under the Media Type enter your json that you want to post. Let's keep it simple and create a JSON value that contains one name/value pair string: {“name”:”JoeColantonio”}


  2. Submit the Request 1. Notice in the response how it returns the value ok and an id
  3. Go back to couchDB. The soapui db should now have a value of 1 and should contain the name you just submitted:


Rest Easy

That's it! You can now rest easy in the knowledge that you now know how to !

10 responses to “soapUI How to Post JSON to a REST Service”

  1. Hi,

    I am getting the following response while posting the JSON content through REST service.Please help me out

    html>

    Error Message

    A {
    FONT-WEIGHT: bold; FONT-SIZE: 10pt; COLOR: #005a80; FONT-FAMILY: tahoma
    }
    A:hover {
    FONT-WEIGHT: bold; FONT-SIZE: 10pt; COLOR: #0d3372; FONT-FAMILY: tahoma
    }
    TD {
    FONT-SIZE: 8pt; FONT-FAMILY: tahoma
    }
    TD.titleBorder {
    BORDER-RIGHT: #955319 1px solid; BORDER-TOP: #955319 1px solid; PADDING-LEFT: 8px; FONT-WEIGHT: bold; FONT-SIZE: 12pt; VERTICAL-ALIGN: middle; BORDER-LEFT: #955319 0px solid; COLOR: #955319; BORDER-BOTTOM: #955319 1px solid; FONT-FAMILY: tahoma; HEIGHT: 35px; BACKGROUND-COLOR: #d2b87a; TEXT-ALIGN: left
    }
    TD.titleBorderx {
    BORDER-RIGHT: #955319 0px solid; BORDER-TOP: #955319 1px solid; PADDING-LEFT: 8px; FONT-WEIGHT: bold; FONT-SIZE: 12pt; VERTICAL-ALIGN: middle; BORDER-LEFT: #955319 1px solid; COLOR: #978c79; BORDER-BOTTOM: #955319 1px solid; FONT-FAMILY: tahoma; HEIGHT: 35px; BACKGROUND-COLOR: #d2b87a; TEXT-ALIGN: left
    }
    .TitleDescription {
    FONT-WEIGHT: bold; FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: tahoma
    }
    SPAN.explain {
    FONT-WEIGHT: normal; FONT-SIZE: 10pt; COLOR: #934225
    }
    SPAN.TryThings {
    FONT-WEIGHT: normal; FONT-SIZE: 10pt; COLOR: #934225
    }
    .TryList {
    MARGIN-TOP: 5px; FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: tahoma
    }
    .X {
    BORDER-RIGHT: #955319 1px solid; BORDER-TOP: #955319 1px solid; FONT-WEIGHT: normal; FONT-SIZE: 12pt; BORDER-LEFT: #955319 1px solid; COLOR: #7b3807; BORDER-BOTTOM: #955319 1px solid; FONT-FAMILY: verdana; BACKGROUND-COLOR: #d1c2b4
    }
    .adminList {
    MARGIN-TOP: 2px
    }

    X

    Network Access Message:
    The page cannot be displayed

    Explanation:

    The Web server refused the connection.

    Try the following:

    Refresh page:
    Search for the page again by clicking the Refresh button. The timeout could have occurred due to Internet congestion.

    Check spelling:
    Check that the Web page address is spelled correctly. The address may have been mistyped.

    Access from a link:
    If there is a link to the page you are looking for, try accessing the page from that link.

    Contact website:
    You may want to contact the website administrator to make sure the Web page still exists. You can do this by using the e-mail address or phone number listed on the website home page.

    If you are still not able to view the requested page, try contacting your administrator or Helpdesk.

    Technical Information (for support personnel)

    Error Code 10061: Connection refused
    Background: The server you are attempting to access has refused the connection with the gateway. This usually results from trying to connect to a service that is inactive on the server.
    Date: 10/30/2013 8:45:58 AM [GMT]
    Server: INICG0029.in.int.atosorigin.com
    Source: Remote server

  2. Hi,

    I am trying to post some json data into a server using soap UI and REST api. I have created a POST request by passing the URL into Endpoint & Resource text fields at the top of soap UI. Parameters text filed i left it as empty. I had given json data like {“type”:”endpoint_resume”} to resume the paused end point but it doesn’t work. Even after running the test case, that particular endpoint was still in “paused” mode. Can some help me on this please ????

    Note : This is working when I used the curl command(post data using curl command) but not working using soap UI. FYI, there is no typo mistakes in URL too.. I need to check something else!!!!!!!!

  3. Great post!

    Well written and easy to follow.

    Helped me get my head around JSON in SOUPUI.

    Thank you for taking the time to do this post and other.

    A

  4. I enjoyed your video as well as the blog. Thanks for sharing. Unfortunately, SoapUI 4.6.4 is not bug free and cannot POST a JSON request: “Cancelled request due to invalid content”. I have resorted to using the RESTClient Add-On for Firefox for just this basic request/response. I might try and download an older version of SoapUI to get back on my feet.

  5. Hi Joe,
    Can you please leave some tips on building a test suite and automating them using SOAPUI and this is specifically for the REST API services and/or using Groovy scripts. I would be glad if I dont have to even use Groovy to setup my automation on SOAPUI.

    Thanks,
    Savitha

  6. if anyone is facing below error.
    cannot POST a JSON request: “Cancelled request due to invalid content”.

    then please go to settings/preferences – > Editor settings
    and uncheck “Abort invalid request” check box

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.