Search code examples
csvpostrequestmultipartform-datakarate

Send a POST method using a CSV as form-data Karate Framework


I need to implement this request in Karate:

curl --location --request POST 'https:/myURL/' \
--header 'Authorization: Bearer myToken' \
--form 'csv=@"newUser.csv"'

PS: newUser.csv file is in the same folder as my feature

This is how I am sending my request on Postman:

Postman request

I tried this:

Scenario: Bulk New Users - csv file with 1 user
  Given header Authorization = accessToken
  And multipart field csv = { read: 'newUser.csv', contentType: 'multipart/form-data' }
  When method post
  Then status 202

But getting 400

I also tried this:

Scenario: Bulk New Users - csv file with 1 user
  Given header Authorization = accessToken
  And form field csv = { read: 'newUser.csv' }
  When method post
  Then status 202

And it's like I'm not sending anything

Finally, if I try this:

Scenario: Bulk New Users - csv file with 1 user
  Given header Authorization = accessToken
  And multipart file csv = { read: 'classpath:newUser.csv'}
  When method post
  Then status 202

Getting: "could not find or read file: classpath:newUser.csv"

Don't know what I am doing wrong here, I see a lot of examples on internet, none of them worked for me, appreciatte your help on this. thanks in advance

EDIT:

I tried the solution given by Peter but now, I am getting a message from my service that only CSV files are allowed:

Error message

Which will be the case when I not attached the csv file or use a file with different extension:

Postman

And this is how it looks the request on Karate logs:

Postman

It seems I am not sending anything as a body after the conversion to string


Solution

  • Make this change:

    * def fileContents = karate.readAsString('classpath:newUser.csv')
    * multipart file csv = { value: '#(fileContents)' }
    

    Refer: https://github.com/karatelabs/karate#csv-files