post

How to use `curl` scripts to test RESTful web services

There may be better ways to do this, but when I was writing a mobile app, with the JavaScript client written in Sencha Touch and the server written with the Play Framework, I wrote some curl scripts to simulate GET, POST, DELETE, and PUT request (method) calls to my Play Framework REST/RESTful web services.

ScalaJ-HTTP examples: GET, POST, handling redirects

Table of Contents1 - Handling redirect requests with ScalaJ-HTTP2 - ScalaJ-HTTP GET request example3 - Setting ScalaJ-HTTP timeout values4 - A ScalaJ-HTTP POST request example5 - ScalaJ-HTTP: Summary

If you ever need some good ScalaJ-HTTP examples, see the test files in the project, including this HttpBinTest.scala file. That file currently shows a number of good ScalaJ-HTTP examples, including GET, POST, redirect examples with Scala.

See that page for a full list of examples, but for my own use, here are a few of them.

How to write a Play Framework POST request web service

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 15.15, “How to write a Play Framework POST request web service.”

Problem

You want to create a web service using the Play Framework that lets users send JSON data to the service using the POST request method.

Solution

Follow the steps from the previous recipe to create a new Play project, controller, and model.

How to send JSON POST data to a REST URL in Scala alvin June 20, 2015 - 5:39pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 15.11, “How to send JSON POST data to a REST URL.”

Problem

When writing Scala code, you want to send JSON data (or other data) to a POST URL, either from a standalone client, or when using a framework that doesn’t provide this type of service.

How to access POST request data with Scalatra

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 15.8, “How to access POST request data with Scalatra.”

Problem

You want to write a Scalatra web service method to handle POST data, such as handling JSON data sent as a POST request.

Solution

To handle a POST request, write a post method in your Scalatra servlet, specifying the URI the method should listen at:

Disabling extra Sencha ExtJS and Touch REST URL parameters

When you first work with a Sencha ExtJS or Touch Store and Proxy, you’ll quickly find that when you create GET and POST REST services, by default the store/proxy adds extra parameters to the end of the URLs you’re accessing.

How to use PHP curl and curl_setopt with JSON web services

Here are two PHP scripts I just wrote that use curl and curl_setopt. The first example makes a GET request, and the second example makes a POST request, and passes JSON data to the web service it accesses.

A PHP curl GET request

This first one makes an HTTP GET request and prints the data that is returned by the URL that it hits:

A curl script to perform a POST to an HTTP web service using JSON data

You can use the following curl script to POST JSON data to a web service:

curl \
  --header "Content-type: application/json" \
  --request POST \
  --data '{"symbol":"GOOG","price":"600.00"}' \
  http://localhost:8080/stocks/saveJsonStock

If you're familiar with the curl command, and JSON web services, this should make sense.

Here are links to other curl shell scripts I've written: