HTTP Connector Example¶ The HTTP Connector enables you to send HTTP or HTTPS requests within an integration sequence. What you'll build¶ This example explains how to use the HTTP Connector to send HTTP requests effectively in your integration projects. If you do not want to configure this yourself, you can simply get the project and run it. Set up the integration project¶ Follow the steps in the Create a Project documentation to set up the integration project. Create a new HTTP connection. Navigate to Project Overview and click on Add Artifact. Select Connections under Other Artifacts. Select HTTP connector. Add the dependencies if not yet added. Use the following values to create the connection. Connection Name - JsonPlaceHolderConnection Base URL - http://jsonplaceholder.typicode.com Auth Type - None Add integration logic¶ Navigate to the Project Overview page and click Add Artifact. Under Create an Integration, select API. Specify the API name as JsonPlaceHolder and the API context as /jsonplaceholder. Create a resource with resource path /getOnePost/{id} and select GET as the method. This resource will retrieve information about a post. Select the created resource, and click the + sign. Then, select the GET operation under the HTTP connector to send a GET request. In the form that opens, provide the following details: Connection - JsonPlaceHolderConnection Relative Path - /posts/${params.pathParams.id} Headers - "" Output Variable Name - http_get_1 Overwrite Message Body - keep selected Click the + icon below the GET operation and add a respond mediator. For the JsonPlaceHolder API, create another resource with resource path /commentsForPost?postId={postId} and select GET as the method. This resource will get comments for a post. Select the created resource, and click the + sign. Then, select the GET operation under the HTTP connector to send a GET request. In the form that opens, provide the following details: Connection - JsonPlaceHolderConnection Relative Path - /comments?postId=${params.queryParams.postId} Headers - "" Output Variable Name - http_get_1 Overwrite Message Body - keep selected Click the + icon below the GET operation and add a respond mediator. For the JsonPlaceHolder API, create another resource with resource path /createPost and select POST as the method. This resource will handle post creation. Select the resource, and click the + sign. Then, select the POST operation under the HTTP connector to send a POST request. In the form that opens, provide the following details: Connection - JsonPlaceHolderConnection Relative Path - /posts Headers - "" Content Type - Select JSON Request Body - ${payload} Output Variable Name - http_post_1 Overwrite Message Body - keep selected Click the + icon below the POST operation and add a respond mediator. Create a resource for the JsonPlaceHolder API with the resource path /replacePost and select PUT as the method. This resource will replace an existing post. Select the resource, and click the + sign. Then, select the PUT operation under the HTTP connector to send a PUT request. In the form that opens, provide the following details: Connection - JsonPlaceHolderConnection Relative Path - /posts/1 Headers - "" Content Type - Select JSON Request Body - { "userId": 1, "id": 1, "title": "christmas holiday", "body": "christmas holiday is coming" } Output Variable Name - http_put_1 Overwrite Message Body - keep selected Click the + icon below the PUT operation and add a respond mediator. Create a resource for the JsonPlaceHolder API with the resource path /updatePost and select PATCH as the method. This resource will update an existing post. Select the resource, and click the + sign. Then, select the PATCH operation to send a PATCH request. In the form that opens, provide the following details: Connection - JsonPlaceHolderConnection Relative Path - /posts/1 Headers - "" Content Type - Select JSON Payload - ${payload} Output Variable Name - http_patch_1 Overwrite Message Body - keep selected Click the + icon below the PATCH operation and add a respond mediator. Create a resource for the JsonPlaceHolder API with the resource path /deletePost and select DELETE as the method. This resource will handle post deletion. Select the resource, and click the + sign. Then, select the DELETE operation to send a DELETE request. In the form that opens, provide the following details: Connection - JsonPlaceHolderConnection Relative Path - /posts/1 Headers - "" Output Variable Name - http_delete_1 Overwrite Message Body - keep selected Click the + icon below the DELETE operation and add a respond mediator. Get the project¶ You can download the ZIP file and extract the contents to get the project code. Deployment¶ Follow these steps to deploy the exported CApp in the integration runtime. Deploying on WSO2 Integrator: MI To deploy and run the project, refer to the Build and Run guide. You can further refer the application deployed through the CLI tool. See the instructions on managing integrations from the CLI. Test¶ HTTP GET operation¶ Test getting a post. Send an HTTP request as below. curl -X 'GET' 'http://localhost:8290/jsonplaceholder/getOnePost/1' -H 'accept: */*' Expected response: { "userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" } Test getting given comments for a post. Send an HTTP request as below. curl -X 'GET' 'http://localhost:8290/jsonplaceholder/commentsForPost?postId=1' -H 'accept: */*' Expected response: [ { "postId": 1, "id": 1, "name": "id labore ex et quam laborum", "email": "[email protected]", "body": "laudantium enim quasi est quidem magnam voluptate ipsam eos\ntempora quo necessitatibus\ndolor quam autem quasi\nreiciendis et nam sapiente accusantium" }, { "postId": 1, "id": 2, "name": "quo vero reiciendis velit similique earum", "email": "[email protected]", "body": "est natus enim nihil est dolore omnis voluptatem numquam\net omnis occaecati quod ullam at\nvoluptatem error expedita pariatur\nnihil sint nostrum voluptatem reiciendis et" }, { "postId": 1, "id": 3, "name": "odio adipisci rerum aut animi", "email": "[email protected]", "body": "quia molestiae reprehenderit quasi aspernatur\naut expedita occaecati aliquam eveniet laudantium\nomnis quibusdam delectus saepe quia accusamus maiores nam est\ncum et ducimus et vero voluptates excepturi deleniti ratione" }, { "postId": 1, "id": 4, "name": "alias odio sit", "email": "[email protected]", "body": "non et atque\noccaecati deserunt quas accusantium unde odit nobis qui voluptatem\nquia voluptas consequuntur itaque dolor\net qui rerum deleniti ut occaecati" }, { "postId": 1, "id": 5, "name": "vero eaque aliquid doloribus et culpa", "email": "[email protected]", "body": "harum non quasi et ratione\ntempore iure ex voluptates in ratione\nharum architecto fugit inventore cupiditate\nvoluptates magni quo et" } ] HTTP POST operation¶ Test creating a post. curl -X 'POST' 'http://localhost:8290/jsonplaceholder/createPost' \ -H 'accept: */*' \ -H 'Content-Type: application/json' \ -d '{ "userId": 200, "id": 1, "title": "new post" }' Expected response: { "userId": 200, "id": 101, "title": "new post" } HTTP PUT operation¶ Test replacing a post. curl -X 'PUT' 'http://localhost:8290/jsonplaceholder/replacePost' \ -H 'accept: */*' \ -H 'Content-Type: application/json' \ -d '{}' Expected response: { "userId": 1, "id": 1, "title": "christmas holiday", "body": "christmas holiday is coming" } HTTP PATCH operation¶ Test updating the post. curl -X 'PATCH' 'http://localhost:8290/jsonplaceholder/updatePost' \ -H 'accept: */*' \ -H 'Content-Type: application/json' \ -d '{ "title": "holiday is over" }' Expected response: { "userId": 1, "id": 1, "title": "holiday is over", "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" } HTTP DELETE operation¶ Test deleting the post. curl -X 'DELETE' 'http://localhost:8290/jsonplaceholder/deletePost' -H 'accept: */*' Expected response: {} How to create a HTTPS Connection?¶ Navigate to the project overview page and click Add Artifact. Under Other Artifacts, select Connections. Choose the HTTPS connection. Use the following values to configure the connection: Connection Name - HTTPSConnection Base URL - HTTPS URL Path Auth Type - None If a certificate is required for the HTTPS connection, it must be added as a certificate type configurable of the project. For more details on adding certificate type configurable, refer to the Externalized Configuration documentation.