1

I am having some difficulties getting a request to work as expected in Karate where I use a .json file in the body of my POST request. I have tried to define it as both a form field and as a multipart file. I assume I'm not doing this correctly because neither of them are getting me the same result as my CURL command. I removed a few of the secret variables, they for sure work in context of the CURL command from Postman

curl --location 'https://{{QA_URL}}/v1/lists/10790' \
--header 'accept: application/json' \
--header 'x-key: {{KEY}}' \
--header 'x-secret: {{SECRET}}' \
--form 'file=@"postman-cloud:///UUID"'

This is my karate scenario in which I do a GET call to retrieve a collection of lists then secondly; to do a POST request to update my specific list - with the couple of failed attempts at declaring the file. The file is located in the file location referenced in my read steps. Thank you for any and all help!

Scenario: GET dataset associated to account
    * def requestBody = read("file:src/test/java/json/User/Request/updateListDetails.json")
    * set requestBody.notes = randomString

    Given path '/v1' + '/lists/'
    And header Content-Type = 'application/json'
    And header x-key = Key
    And header x-secret = Secret
    When method GET
    * def listId = response.lists[2].id

    Given path '/v1' + '/lists/' + listId
    And header accept = 'application/json'
    And header Content-Type = 'multipart/form-data'
    And header x-key = Key
    And header x-secret = Secret
    #And form field file = read("file:src/test/java/json/User/Request/updateListDetails.json")
    #And multipart field file = { read: 'file:src/test/java/json/User/Request/updateListDetails.json', filename: 'updateListDetails.json', contentType: 'multipart/form-data' }
    #And request requestBody
    When method POST
    Then status 200

1 Answer 1

1

Try using the multipart file:

Feature: Upload file to the list

Background:
    * url 'https://{{QA_URL}}/v1'
    * def headers = { 
        'accept': 'application/json',
        'x-key': '{{KEY}}',
        'x-secret': '{{SECRET}}'
      }

  Scenario: Upload file to the list
    Given path 'lists/10790'
    And headers headers
    And multipart file file = { read: 'postman-cloud:///UUID', filename: 'UUID', contentType: 'application/octet-stream' }
    When method POST
    Then status 200
    And match response == { some: 'expectedResponse' }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.