Richa
Richa

Reputation: 31

Unable to set multiple headers - Content-Type & Accept while using karate

The request that I am trying to test is having 2 mandatory headers - Content-Type: 'application/json' and Accept: 'application/json'.

When I run below scenario , it gives me status code 400 saying Mandatory header Accept is missing - which is correct behavior.

Scenario: Validating test

  * configure headers = { Content-Type: 'application/json'}
And url testURL
  And request { "abc": {"xyz": "fgfdgf"}}
  When method post

But , when I run below scenario , it still gives me status code 400 saying Mandatory header Content-Typeis missing - which is NOT right behavior.

Scenario: Validating test

  * configure headers = { Content-Type: 'application/json',Accept:'application/json'}
And url testURL
  And request { "abc": {"xyz": "fgfdgf"}}
  When method post
  Then status 200

Can someone suggest what is wrong in the second scenario?

Upvotes: 2

Views: 949

Answers (2)

Richa
Richa

Reputation: 31

This issue was resolved by configuring charset as null :

  • configure charset = null

By default , charset is taken as UTF-8. So in order to explicitly define content-type we need to set charset as null in Background.

Upvotes: 1

Peter Thomas
Peter Thomas

Reputation: 58058

Note that Content-Type will be automatically set by Karate, you don't need to set it - if the request body is clearly JSON.

The mistake you are making is that the - character is not valid as a JSON (JS) key-name, so this would work:

* configure headers = { 'Content-Type': 'application/json', Accept: 'application/json' }

Observe the quotes around Content-Type.

Upvotes: 1

Related Questions