简体   繁体   中英

How can we make a POST service call which takes formdata instead of json in OpenTest?

We have a WebService which takes formData key-value pair as the request instead of json. Using openTest how can we pass these formData ? Basically we need a code snippet to post the formData using OpenTest yaml script.

Below is the sample curl command which we need to post using OpenTest which has Content-Type as multipart/form-data

`
curl --location --request POST 'https://serviceurl.com/getacb' \
--form 'userKey=a1b23' \
--form 'apiKey=1_ffER_hk6Rb89--2EElfsdeF3' \
--form 'secret=Ude+6NIjojo89/gyAB7huGS5' \
--form 'targetUID=ulknnk4kjlkj5'
`

We are looking for a sample snippet to post the above multipart/form-data.

You'll need to build the FormData() object yourself before passing it to the next action.

var data = new FormData();
data.append("userKey", "a1b23");
data.append("apiKey", "1_ffER_hk6Rb89--2EElfsdeF3");
data.append("secret", "Ude+6NIjojo89/gyAB7huGS5");
data.append("targetUID", "ulknnk4kjlkj5");

This is the sample with some modifications from OpenTest API testing YAML .

description: Example Post based off SO Question
actors:
  - actor: ACTOR1
    segments:
      - segment: 1
        actions:
          - description: Create a random post ID
            script: | 
              var data = new FormData();
              data.append("userKey", "a1b23");
              data.append("apiKey", "1_ffER_hk6Rb89--2EElfsdeF3");
              data.append("secret", "Ude+6NIjojo89/gyAB7huGS5");
              data.append("targetUID", "ulknnk4kjlkj5");

          - description: Send a request to getacb
            action: org.getopentest.actions.HttpRequest
            args:
              url: https://serviceurl.com/getacb
              headers:
                Content-Type: multipart/form-data
              verb: POST
              body: data

          - description: Extract the response's status code and body
            script: |
              var statusCode = $output.statusCode;
              var postInfo = $output.body;

          - description: Validate the response status code
            script: |
              if (statusCode != 201) {
                $fail($format(
                  "We expected the status code to be {0} but it was {1}",
                  201,
                  statusCode));
              }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM