'How to do HTTP form POST requests with karate reading key-values from a json file

I am trying to do a series of form POST requests using karate, each form has different fields. So I am trying to read the actual key-value pair data for the form input fields from a json file.

When submitting the form in a browser, the equivalent curl looks like this:

curl 'http://test.example.com:8080/submitform.html' -X POST -H 'Content-Type: multipart/form-data; boundary=---------------------------12345' --data-binary $'-----------------------------12345\r\nContent-Disposition: form-data; name=":formid"\r\n\r\n_submitform_start\r\n-----------------------------12345\r\nContent-Disposition: form-data; name=":formstart"\r\n\r\n/submitform/start\r\n-----------------------------12345\r\nContent-Disposition: form-data; name="_charset_"\r\n\r\nUTF-8\r\n-----------------------------12345\r\nContent-Disposition: form-data; name=":redirect"\r\n\r\n/submitform/thank-you.html\r\n-----------------------------12345\r\nContent-Disposition: form-data; name="xyzHidden"\r\n\r\n\r\n-----------------------------12345\r\nContent-Disposition: form-data; name="first-name"\r\n\r\ntestfirst\r\n-----------------------------12345\r\nContent-Disposition: form-data; name="last-name"\r\n\r\ntestlast\r\n-----------------------------12345\r\nContent-Disposition: form-data; name="email"\r\n\r\[email protected]\r\n-----------------------------12345\r\nContent-Disposition: form-data; name="telephone-number-with-country-code-optional"\r\n\r\n\r\n-----------------------------12345\r\nContent-Disposition: form-data; name="company-name"\r\n\r\ntestcompany\r\n-----------------------------12345\r\nContent-Disposition: form-data; name="city"\r\n\r\ntestcity\r\n-----------------------------12345\r\nContent-Disposition: form-data; name="country-region"\r\n\r\nAustralia\r\n-----------------------------12345\r\nContent-Disposition: form-data; name="describe-your-question-or-situation"\r\n\r\ntest describe\r\n-----------------------------12345\r\nContent-Disposition: form-data; name="uploadNode_upload"\r\n\r\n/submitform/upload\r\n-----------------------------12345\r\nContent-Disposition: form-data; name="upload"; filename=""\r\nContent-Type: application/octet-stream\r\n\r\n-----------------------------12345--\r\n'

My feature file looks like this:

Feature: Form POST submit from file

  Background:
    * url sitehost

  Scenario Outline: form submit
    Given path '<submitpath>'
    And def ffParams = read('classpath:forms/<datafile>.json') 
    And multipart fields ffParams
    When method post
    Then status 302
    And match header Location == sitehost + '<thankyoupath>'

    Examples:
    | submitpath | datafile | thankyoupath |
    | /submitform.html | submitform_data | submitform/thank-you.html |

My data json file looks something like this:

{
":formid":"_submitform_start",
":formstart":"/submitform/start",
"_charset_":"UTF-8",
":redirect":"/submitform/thank-you.html",
"xyzHidden":"",
"first-name":"testfirst",
"last-name":"testlast",
"email":"[email protected]",
"telephone-number-with-country-code-optional",
"company-name":"testcompany",
"city":"testcity",
"country-region":"Australia",
"describe-your-question-or-situation":"test describe"
}

I would like to submit the data as multipart/form-data, not application/x-www-form-urlencoded. According to the docs this is possible by using "multipart fields".

But when I do the above, in my request karate will put a filename="" after each key, e.g. it looks like

content-disposition: form-data; name="first-name"; filename=""
content-type: text/plain; charset=UTF-8
content-length: 4
Completed: true
IsInMemory: true

but it should look like in the curl

Content-Disposition: form-data; name="first-name"\r\n\r\ntestfirst\r\n

So not working as expected



Solution 1:[1]

First:

* def foo = null
* def bar = { a: 1, b: '##(foo)' }
* match bar == { a: 1 }

Note how b got removed. Explained in the docs: https://github.com/karatelabs/karate#remove-if-null

There are plenty of ways to manipulate JSON such as remove.

Regarding file-upload, this is notoriously tricky, so try to get things working without it if possible and then refer this thread.

Also see: https://stackoverflow.com/a/71512836/143475

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Thomas