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 - Content-type header can't be set for POST and PUT requests

See original GitHub issue

Current behaviour:

I’m passing to the cy.request command the options object that contains the header Content-type:

cy.request({
  method: 'POST',
  url: `${Cypress.env('RAILS_APP_URL')}/admin/api/clients`,
  headers: {
    'Content-Type': 'application/json; charset=utf-8',
  },
  body: {}
})

For some reason, the header is not sent to the server. In the console, I can verify that the request doesn’t have Content-Type :

screenshot_6

The same is true for PUT verb:

cy.request({
  method: 'PUT',
  url: `${Cypress.env('RAILS_APP_URL')}/admin/api/employees/54967`,
  body: {},
  headers: {
    'Content-Type': 'application/json; charset=utf-8',
  }
})

screenshot_7

The only way I can force to set the content-type header is to add to each URL .json postfix eg. ${Cypress.env('RAILS_APP_URL')}/admin/api/clients.json

I can’t find any information in the docs that Content-Type is ignored and won’t be set for POST/PUT request. When I set it for GET a request it is set correctly.

Desired behaviour:

When I set Content-Type header in the cy.request command, it is sent with the request.

Versions

cypress 4.7.0 operating system: Linux Ubuntu 18 LTS browser: Chrome 83

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Olgagrcommented, Jun 30, 2020

@jennifer-shehane I only noticed one difference that I can reproduce. In case of your request, there is no referer header sent. In case of request to our API, the referer is always sent. It seems connected. When the request headers contains referer header, the Content-Type is not sent when set as an option.

As far as I can see our API sends Referrer-Policy header set to strict-origin-when-cross-origin

1reaction
gitsaquibcommented, Jul 30, 2020

Can we override referer? I dont want Referer like localhost:port/_cypress… I want to set this to say https://jira.myjira.com

Read more comments on GitHub >

github_iconTop Results From Across the Web

request
A request body to be sent in the request. Cypress sets the Accepts request header and serializes the response body by the encoding...
Read more >
cypress-io/cypress - Gitter
When making a cy.request(method: "POST"), it seems to be executed and it can be logged and everything, but it does not appear in...
Read more >
Test with cypress an custom HTTP Headers
Create a login() inside commands.js and use cy.request() to login to system and add the headers. Also before the test, I have run...
Read more >
Cypress request and cookies | Better world by better software
While running the Cypress tests you can make requests to the backend with full set of the page's cookies using the cy.request command....
Read more >
Content-Type - HTTP - MDN Web Docs - Mozilla
In requests, (such as POST or PUT ), the client tells the server what ... values can't contain a CORS-unsafe request header byte:...
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