question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Code 418 is returned instead of 200 if not using "And header Accept = 'application/json'"

See original GitHub issue

Hi 😃 I am new to API testing and I was testing out Karate to practice API testing but there was a weird thing that happened and I don’t know if it is an issue or a misunderstanding from my side.

I am trying testing against “https://restful-booker.herokuapp.com/” APIs … It is An API playground created by Mark Winteringham for those wanting to learn more about API testing and tools

I added my feature as follows:

  Scenario: POST /booking should return 200 status code
    Given url 'https://restful-booker.herokuapp.com/booking'
    And request read('post_booking_data.json')
    When method POST
    Then status 200

It didn’t work and returned “418” although when I try the exact thing with postman, the 200 status code is returned successfully. But then after reading another part in Karate documentation, I updated my feature to be as follows:

  Scenario: POST /booking should return 200 status code
    Given url 'https://restful-booker.herokuapp.com/booking'
    And request read('post_booking_data.json')
    And header Accept = 'application/json'
    When method POST
    Then status 200

This just went well and the test ran successfully only after I added “And header Accept = ‘application/json’”

Is there anything that I was doing wrong or I didn’t understand it correctly?

Thanks a lot!!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
AyaAkl25commented, May 6, 2018

@ptrthomas I would like to thank you a lot for your answer. And I think, yes, the API I am trying on is badly designed as you said.

1reaction
ptrthomascommented, May 6, 2018

@Aya-Akl I think Karate is working as designed. There are 2 things that are happening. First: while Karate will send the Content-Type header automatically depending on the type of the payload, Postman seems to go one step further and set the Accept header as well.

Second: the API here is (at least in my opinion) badly designed. Ideally, the API should assume the intended Accept to be same as the Content-Type if not specified by the user. Here is an example of another API (not created by Mark Winteringham 😉 that works as you would expect:

Scenario: POST /booking should return 201 status code
Given url 'https://jsonplaceholder.typicode.com/users'
And request
"""
{
  "name": "Test User",
  "username": "testuser",
  "email": "test@user.com",
  "address": {
    "street": "Has No Name",
    "suite": "Apt. 123",
    "city": "Electri",
    "zipcode": "54321-6789"
  }
}
"""
When method post
Then status 201

You can also try setting the Accept to application/xml in your example and see what happens.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Code 418 is returned instead of 200 if not using "And header ...
Code 418 is returned instead of 200 if not using "And header Accept ... header Accept = 'application/json' When method POST Then status...
Read more >
Response code 418 when trying to web scrape the API
Request invokes an HTTP get request, but sometimes the server requires a user-agent, partial/complete headers, parameters or cookies to ...
Read more >
Reserve the 418 status code - Hacker News
I wanted a status code on the API that could only possibly be generated on purpose, so if it receives a 418 response...
Read more >
415 Unsupported Media Type - HTTP - MDN Web Docs
The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload ...
Read more >
How to Return HTTP Status Codes in a Spring Boot Application
If the request went through just fine, a 200 OK is returned, while a 404 Not Found is returned if the resource isn't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found