One way to define a parseArgs method to parse request and header parameters is shown in the following listing. The request parameters and header parameters are determined by a prefix parameter, –d or –h. The input to the script will look something like the following: I got the NameValuePair code from the URL I've linked to. Although its written in Scala, it uses the Apache HttpClient Java libraries. Last updated: JI created this Scala class as a way to test an HTTP POST request to a web service. The first parameter to the script is the command, and the next ones are the request and header parameters. A Scala HTTP POST client example (like Java, uses Apache HttpClient) By Alvin Alexander. In this case, because you expect at least two parameters, anything less than that will result in an exception. The require function throws an exception when the predicate evaluates to false. Remember that the command-line inputs are represented by an args array. You’re using a require function defined in Predef to check the size of the input. http http-client scala Scala versions: 2.13 2.12 Project 25 Versions Badges Scala 2.13 scala-http-client 4.7.0 Group ID: io.moia Artifact ID: scala-http-client2. "at minimum you should specify action(post, get, delete, options) and url" ).To execute an HTTP DELETE method you have to do the following: HttpClient provides a default client and is good enough for our purpose. In this script you’re going to use four types of requests: GET, POST, DELETE, and OPTIONS.11 The previous example implemented only GET, POST, and DELETE because the web container will automatically implement the OPTIONS method. HttpClient encapsulates each HTTP method type in an object, and they’re available under the .methods package. Users are supposed to provide a request object like HttpPost or HttpGet, and the HttpClient is expected to transmit the request to the target server and return the corresponding response object, or throw an exception if the execution is unsuccessful. The most essential function of HttpClient is to execute HTTP methods. It’s not a browser, and it doesn’t execute JavaScript or try to guess the content type or other functionality unrelated to the HTTP transport. The purpose of HttpClient is to transmit and receive HTTP messages. HttpClient is a client-side HTTP transport library. The simple way to create a RESTful service for now is to use a Java servlet, as shown in the following listing. You need to access the HTTP response headers after making an HTTP request in your Scala code. This is a very short recipe, Recipe 15.12, How to access HTTP response headers after making an HTTP request with Apache HttpClient. Understanding how the service is implemented isn’t important for this example. This is an excerpt from the Scala Cookbook (partially modified for the internet). I’ll use a Java servlet ( Java developers are familiar with it ) to build the service to test the REST client. You could use any REST tool or a framework to build the REST service. You could use free public web services to test the client, but to have better control of the operations on the service you’ll create one. To test your client you need a RESTful web service. Additionally, other client libraries can be used to invoke REST requests. Similarly, Scala also provides many open-source REST libraries, such as Akka-HTTP, Play Framework, http4s, ZIO Http, and Finch. To make a REST call to a RESTful service, you have to be aware of the operations supported by the service. Most programming languages have libraries to build and consume RESTful services. You’re going to use the Apache HttpClient library to handle HTTP connections and various HTTP methods. In this section you’ll build a command-line-based REST client in Scala script. I cannot figure out what is going wrong.Command-line REST client: building a working example You’ve looked into a number of interesting concepts about Scala in this chapter, and it’ll be nice to see some of these concepts in action together. We’re matching against a Request, so let’s look closer at the case statement above. I printed post.txt in scala and got the good content. We will use A and B here, knowing that above A Request and B is a PartialCollectA - which behaves like a PartialFunction, meaning we’re going to pattern match on something relating to A and return a B. The curl action which is working is done from my scala project directory. Val rspStr=Source.createBufferedSource().mkStringīut I get an HTTP/1.1 500 Internal Server ErrorĪnd printing the rspStr yields "?xml version="1.0" encoding="utf-8"?>soap:ReceiverServer was unable to process request. Val httpResponse=httpClient.execute(postRequest) Val fe=new FileEntity(file,"application/soap xml charset=utf-8") PostRequest.addHeader("SOAPAction","\"\"") PostRequest.addHeader("Content-Type","application/soap xml charset=utf-8") Now I want to call the API programatically with a simple HttpClient val httpClient = new DefaultHttpClient() I first did it with a curl like this : curl -X POST -T post.txt -H "Content-Type: application/soap xml charset=utf-8" -v I want to make a post request in Scala to this API
0 Comments
Leave a Reply. |