Skip to content

Add Content-Type to finalize request in KV client #1147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

pavelm10
Copy link
Contributor

@pavelm10 pavelm10 commented Nov 6, 2022

Fixes #1146

@bmc-msft bmc-msft self-requested a review November 7, 2022 14:16
@@ -21,7 +21,8 @@ impl SetSecretBuilder {

let body = Some(Value::Object(request_body).to_string().into());

let headers = Headers::new();
let mut headers = Headers::new();
headers.insert(CONTENT_TYPE, "application/json");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to be a larger problem than just SetSecretBuilder. Setting the content type was mistakenly dropped in #962.

Instead of updating just SetSecretBuilder, can you instead update finalize_request?

Perhaps something like this:

--- a/sdk/security_keyvault/src/clients/keyvault_client.rs
+++ b/sdk/security_keyvault/src/clients/keyvault_client.rs
@@ -4,7 +4,8 @@ use azure_core::{
     date,
     error::{Error, ErrorKind},
     headers::*,
-    Body, Context, Method, Pipeline, Request, Response,
+    prelude::*,
+    Body, Method, Pipeline, Request, Response,
 };
 use const_format::formatcp;
 use std::sync::Arc;
@@ -84,6 +85,9 @@ impl KeyvaultClient {
         request.insert_header(MS_DATE, time);

         if let Some(request_body) = request_body {
+            if request.headers().get_optional_str(&CONTENT_TYPE).is_none() {
+                request.insert_headers(&ContentType::APPLICATION_JSON);
+            }
             request.insert_header(CONTENT_LENGTH, request_body.len().to_string());
             request.set_body(request_body);
         } else {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, if you'd prefer, let me know and I can make the broader change.

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do it, I was just thinking that the other operations (get, delete, get_versions) are working without the Content-Type header, hence putting it into the common finalize_request method seemed incorrect to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but your suggestion make sense that for request with body it should be added, in case in the future there are other operation which need to set the header. I will change it according to your suggestion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get & delete do not set request bodies, which makes this less of an issue.

@pavelm10 pavelm10 force-pushed the bugfix/set-secret-missing-content-type-header branch from 14ce7e2 to aa3868d Compare November 7, 2022 16:56
@pavelm10 pavelm10 changed the title Add Content-Type to SetSecretBuilder request Add Content-Type to finalize request in KV client Nov 7, 2022
@pavelm10 pavelm10 requested a review from bmc-msft November 7, 2022 16:57
@bmc-msft bmc-msft merged commit 897ce67 into Azure:main Nov 7, 2022
@bmc-msft
Copy link
Contributor

bmc-msft commented Nov 7, 2022

Thanks for the PR!

@pavelm10
Copy link
Contributor Author

pavelm10 commented Nov 7, 2022

No problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Set KeyVault secret is missing Content-Type header attribute
2 participants