Skip to content

Commit

Permalink
Auto merge of #13264 - Turbo87:ct-header, r=weihanglo
Browse files Browse the repository at this point in the history
crates-io: Set `Content-Type: application/json` only for requests with a body payload

### What does this PR try to resolve?

The `Content-Type` **request** header is only supposed to be used if the request comes with a body payload. `cargo` is currently sending it unconditionally, even for `GET` requests that typically don't have a payload attached to them.

This PR changes the implementation to only attach the header if the request has a body payload.
  • Loading branch information
bors committed Jan 9, 2024
2 parents b90f770 + 6218d08 commit 616bae2
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ cargo_metadata = "0.18.1"
clap = "4.4.12"
color-print = "0.3.5"
core-foundation = { version = "0.9.4", features = ["mac_os_10_7_support"] }
crates-io = { version = "0.39.0", path = "crates/crates-io" }
crates-io = { version = "0.40.0", path = "crates/crates-io" }
criterion = { version = "0.5.1", features = ["html_reports"] }
curl = "0.4.44"
curl-sys = "0.4.70"
Expand Down
2 changes: 1 addition & 1 deletion crates/crates-io/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "crates-io"
version = "0.39.2"
version = "0.40.0"
rust-version.workspace = true
edition.workspace = true
license.workspace = true
Expand Down
4 changes: 3 additions & 1 deletion crates/crates-io/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,9 @@ impl Registry {
self.handle.url(&format!("{}/api/v1{}", self.host, path))?;
let mut headers = List::new();
headers.append("Accept: application/json")?;
headers.append("Content-Type: application/json")?;
if body.is_some() {
headers.append("Content-Type: application/json")?;
}

if self.auth_required || authorized == Auth::Authorized {
headers.append(&format!("Authorization: {}", self.token()?))?;
Expand Down
2 changes: 1 addition & 1 deletion src/doc/src/reference/registry-web-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ be required in the future.

Cargo sets the following headers for all requests:

- `Content-Type`: `application/json`
- `Content-Type`: `application/json` (for requests with a body payload)
- `Accept`: `application/json`
- `User-Agent`: The Cargo version such as `cargo 1.32.0 (8610973aa
2019-01-02)`. This may be modified by the user in a configuration value.
Expand Down

0 comments on commit 616bae2

Please sign in to comment.