cy.request causing 400 status due to missing Host header
See original GitHub issueCurrent 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:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I checked the rfc7230 5.4.
And it says that:
And Cypress is ignoring this.
Hi! I’m having a similar issue. The request I’m sending is like follows:
And it fails:
And the status code from the response is 404.
When I try to set the host header as follows:
The log from Cypress was:
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:
And the log was:
Again, Cypress seems to correctly send any header I set, except for host, that for some reason is missing there.
Cypress version:
Any help will be welcome.
Thanks!