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.

cy.request causing 400 status due to missing Host header

See original GitHub issue

Current behavior

I am using cy.request to simulate user behaviour.

It appears that Cypress does not send the Host header, which makes my (cloudflare-gated) api reject the request.

https://my_apicall.com/

The response we received from your web server was:

  > 400: Bad Request

This was considered a failure because the status code was not `2xx` or `3xx`.

If you do not want status codes to cause failures pass the option: `failOnStatusCode: false`

-----------------------------------------------------------

The request we sent was:

Method: GET
URL: https://my_apicall.com/
Headers: {
  "Connection": "keep-alive",
  "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Cypress/9.4.1 Chrome/94.0.4606.81 Electron/15.3.4 Safari/537.36",
  "accept": "*/*",
  "accept-encoding": "gzip, deflate",
  "content-type": "application/json",
  "content-length": 83,
  "referer": "https://my_apicall.com/"
}

Redirects: [
  "307: https://my_apicall.com/expected_redirect"
]

-----------------------------------------------------------

The response we got was:

Status: 400 - Bad Request
Headers: {
  "date": "Mon, 21 Feb 2022 14:14:55 GMT",
  "content-type": "text/html; charset=UTF-8",
  "transfer-encoding": "chunked",
  "connection": "keep-alive",
  "referrer-policy": "no-referrer",
  "accept-ranges": "bytes",
  "x-served-by": "cache-iad-kiad7000064-IAD",
  "x-cache": "MISS",
  "x-cache-hits": "0",
  "x-timer": "S1645452895.319054,VS0,VE0",
  "cf-cache-status": "DYNAMIC",
  "expect-ct": "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"",
  "report-to": "{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=wYuVS5epYzpLY8Q4hM8ZId0we2tHSErWBDPwW7tLSOmWHAfExOyK8y27u9bPocmpg5oYp2xAcKgOvSMfjo1nT2vcU077sCIfdYD70PO5cePeF%2BDjvSIcB0sz4OqC7rg%3D\"}],\"group\":\"cf-nel\",\"max_age\":604800}",
  "nel": "{\"success_fraction\":0,\"report_to\":\"cf-nel\",\"max_age\":604800}",
  "server": "cloudflare",
  "cf-ray": "6e109573bdfe5a46-IAD",
  "alt-svc": "h3=\":443\"; ma=86400, h3-29=\":443\"; ma=86400"
}
Body: <!DOCTYPE html>

Desired behavior

Send the Host header so that they are valid according to rfc7230 5.4.

Test code to reproduce

Attempt to call an API that forces strict RFC compliance: fail.

Manually add Host using cy.request( { headers: { Host: ... } } ): success.

Cypress Version

9.5.0

Other

No response

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
sainthkhcommented, Apr 11, 2022

I checked the rfc7230 5.4.

And it says that:

A client MUST send a Host header field in all HTTP/1.1 request messages.

And Cypress is ignoring this.

0reactions
Urhy3lcommented, Oct 19, 2022

Hi! I’m having a similar issue. The request I’m sending is like follows:

cy.request({
    method: 'POST',
    url: 'http://myUrl.com/a/nice/path',
    body: {email: email},
});

And it fails:

The request we sent was:

Method: POST
URL: http://backend-openpay-pe.test.geopagos.com/api/registrations/send-code
Headers: {
  "Connection": "keep-alive",
  "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/106.0.5249.119 Safari/537.36",
  "accept": "*/*",
  "accept-encoding": "gzip, deflate",
  "referer": "aNiceReferer"
}
Redirects: [
  "308: http://myUrl.com/a/nice/path"
]

And the status code from the response is 404.

When I try to set the host header as follows:

cy.request({
  method: 'POST',
  headers: {
    host: 'myUrl.com',
  },
  url: 'http://myUrl.com/a/nice/path',
})

The log from Cypress was:


The request we sent was:

Method: POST
URL: http://backend-openpay-pe.test.geopagos.com/api/registrations/send-code
Headers: {
  "Connection": "keep-alive",
  "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/106.0.5249.119 Safari/537.36",
  "accept": "*/*",
  "accept-encoding": "gzip, deflate",
  "referer": "http://myUrl.com/a/nice/path"
}
Redirects: [
  "308: http://myUrl.com/a/nice/path"
]

And again, a response with 404. What I can see in the previous response, is that Cypress is not sending the host header, even when I manually set it. I tried testing some other headers:

cy.request({
  method: 'POST',
  headers: {
    host: 'myUrl.com',
    banana: 'MonkeyLikesBananas',
  },
  url: 'http://myUrl.com/a/nice/path',
})

And the log was:

The request we sent was:

Method: POST
URL: http://backend-openpay-pe.test.geopagos.com/api/registrations/send-code
Headers: {
  "Connection": "keep-alive",
  "banana": "MonkeyLikesBananas",
  "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/106.0.5249.119 Safari/537.36",
  "accept": "*/*",
  "accept-encoding": "gzip, deflate",
  "referer": "http://myUrl.com/a/nice/path"
}
Redirects: [
  "308: http://myUrl.com/a/nice/path"
]

Again, Cypress seems to correctly send any header I set, except for host, that for some reason is missing there.

Cypress version:

$ npx cypress -v      
Cypress package version: 10.9.0
Cypress binary version: 10.9.0
Electron version: 19.0.8
Bundled Node version: 16.14.2

Any help will be welcome.

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTTP status code to signal bad or missing Host header
The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived...
Read more >
Why is cy.intercept() causing my API request to return a 400 ...
cy. intercept() only seems to cause this issue on GET requests, not POST ones. The intercepted request will return a 400 response.
Read more >
request - Cypress Documentation
cy.request() yields the response as an object literal containing properties such as: status; body; headers ...
Read more >
400 Bad Request - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request ......
Read more >
How to Fix a 400 Bad Request Error (Causes and Fixes) - Kinsta
The HTTP error 400 can occur due to incorrectly typed URL, malformed syntax, or a URL that contains illegal characters. This is surprisingly ......
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