Skip to content

Commit 07c9cd5

Browse files
committed
fix: setting proper content-type when http request is not GET
1 parent 220162a commit 07c9cd5

File tree

1 file changed

+16
-11
lines changed
  • src/agent/namespaces/http

1 file changed

+16
-11
lines changed

src/agent/namespaces/http/mod.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl Action for SetHeader {
8686
struct Request {}
8787

8888
impl Request {
89-
async fn create_url_from(state: &SharedState, payload: Option<String>) -> Result<Url> {
89+
async fn create_target_url_from(state: &SharedState, payload: Option<String>) -> Result<Url> {
9090
let req_page = payload.unwrap();
9191
let lock = state.lock().await;
9292
let mut http_target = if let Some(val) = lock.get_variable("HTTP_TARGET") {
@@ -186,35 +186,40 @@ impl Action for Request {
186186
attrs: Option<HashMap<String, String>>,
187187
payload: Option<String>,
188188
) -> Result<Option<String>> {
189+
// create a parsed Url from the attributes, payload and HTTP_TARGET variable
189190
let attrs = attrs.unwrap();
190191
let method = reqwest::Method::from_str(attrs.get("method").unwrap())?;
191-
let parsed = Self::create_url_from(&state, payload.clone()).await?;
192-
let query_str = parsed.query().unwrap_or("").to_string();
192+
let target_url = Self::create_target_url_from(&state, payload.clone()).await?;
193+
let query_str = target_url.query().unwrap_or("").to_string();
193194

194195
// TODO: handle cookie/session persistency
195196

196-
let mut client = reqwest::Client::new().request(method.clone(), parsed.clone());
197-
let lock = state.lock().await;
198-
let headers = lock.get_storage("http-headers")?;
197+
let mut request = reqwest::Client::new().request(method.clone(), target_url.clone());
199198

200-
for (key, value) in headers.iter() {
201-
client = client.header(key, &value.data);
199+
// add defined headers
200+
for (key, value) in state.lock().await.get_storage("http-headers")?.iter() {
201+
request = request.header(key, &value.data);
202202
}
203203

204204
// if there're parameters and we're not in GET, set them as the body
205205
if !query_str.is_empty() && !matches!(method, reqwest::Method::GET) {
206-
client = client.body(query_str);
206+
request = request.header(
207+
reqwest::header::CONTENT_TYPE,
208+
"application/x-www-form-urlencoded",
209+
);
210+
request = request.body(query_str);
207211
}
208212

209213
log::info!(
210214
"{}.{} {} ...",
211215
"http".bold(),
212216
method.to_string().yellow(),
213-
parsed.to_string(),
217+
target_url.to_string(),
214218
);
215219

220+
// perform the request
216221
let start = Instant::now();
217-
let res = client.send().await?;
222+
let res = request.send().await?;
218223
let elaps = start.elapsed();
219224

220225
return if res.status().is_success() {

0 commit comments

Comments
 (0)