在空手道的同一场景中的多个调用中使用 header

Use header in multiple calls in the same scenario in Karate

我的一项功能只有一个场景且有多个 http 调用,我想对所有调用使用相同的主机和 headers。但是,虽然我可以将 url 设置为适用于所有调用,但 header 似乎只适用于第一次调用,然后重置。有人知道为什么会发生这种情况吗and/or关于如何正确执行此操作的建议(除了分别在每个调用中添加它们之外)?

通过在 Background 中设置它们或使用通用 Given,url 用于两个调用,但 header 仅包含在第一个调用中: 1)

Feature: sample

  Background:
  * header Content-Type = 'application/json' 
  * url http://localhost:8080

  Scenario: do multiple calls
    Given path /sample/
    When method GET
    Then status 200

    Given path /sample2/
    When method GET
    Then status 200

2)

Feature: sample2

Given header Content-Type = 'application/json'
And url http://localhost:8080 

  Scenario: do multiple calls
    Given path /sample/
    When method GET
    Then status 200

    Given path /sample2/
    When method GET
    Then status 200

您真的应该阅读文档:https://github.com/intuit/karate#configure-headers

就这样:

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

还有更多选项,请阅读文档。请注意,您通常不需要设置 Content-Type,因为空手道会根据请求正文自动执行此操作。