Forum Discussion

JKowalczyk's avatar
JKowalczyk
Occasional Contributor
14 years ago

Can't get REST request headers and body content using events

Hi All,

I'm trying to create a helper class to transform any REST request into a cURL command (very handy for DEVs to reproduce the bug );
I'm aiming at writing a code that will generate such cURL commands for each REST request send no matter what test runner was used (test suite, test case, or test step).

Let's start with configuring SoapUI:

  • Firstly, put my CurlGenerator.groovy script in your script library folder, using such directory structure: script_lib_folder/com/yelllabs/soapy/helpers/CurlGenerator.groovy

  • Then, place a custom listener (please find it attached - curl-listeners.xml) in bin/listeners folder.

  • After that add a new event in project properties Events tab:
    name: RequestFilter.afterRequest
    event: RequestFilter.afterRequest

  • followed by two lines of code to execute my code:
    import com.yelllabs.soapy.helpers.CurlGenerator;
    new CurlGenerator(context, request, log);

    ps. this constructor will automatically print the command, so there's no point in invoking any further method or assigning it to a field.


OK, so now we've all files and config in place and the interesting part starts

As you remember I want to generate cURL command no matter how you execute your test (using test suite, test case or test step runner)

Let's start with submitting request by manually running a test step:
When you execute it for the first time, then in the "Script log" tab you should see a cURL command but with missing HTTP headers:
Thu Jul 05 16:32:49 BST 2012:INFO:REQUEST HEADERS: [:]
Thu Jul 05 16:32:49 BST 2012:INFO:curl -ki -XPOST "https://hostname/api/categories" -d '{
"asdasd":"asdasd"
}'


If I submit the same request for the second time, then I'll get what I want, so all the headers and msg body:
Thu Jul 05 16:33:22 BST 2012:INFO:REQUEST HEADERS: [:]
Thu Jul 05 16:33:22 BST 2012:INFO:curl -ki -XPOST "https://hostname/api/categories" -H "Host:hostname" -H "Content-Length:22" -H "Accept-Encoding:gzip,deflate" -H "User-Agent:Apache-HttpClient/4.1.1 (java 1.5)" -H "Connection:Keep-Alive" -H "Content-Type:application/json;charset=UTF-8" -H "Accept:application/json" -d '{
"asdasd":"asdasd"
}'

( I don't know why, but I have a feeling that those headers are coming from the previous request and not from the current one but that just a feeling )

Now, let's send that request but by using a test case and test suite runner:
No matter how many times I run, it it'll be always missing all the headers:
Thu Jul 05 17:00:40 BST 2012:INFO:REQUEST HEADERS: [:]
Thu Jul 05 17:00:40 BST 2012:INFO:curl -ki -XPOST "https://hostname/api/categories" -d '{
"asdasd":"asdasd"
}'



Can you help me please with this problem???
I've spend so many hours studying the docs, that I had it enough for next few months

aaaah I almost forgot, sometimes I can't access the msg body too, but I can't reproduce that yet

Cheers,
Janusz
No RepliesBe the first to reply