0

Hi i want to send a tar archive with a docker image to docker build api. this is an example of how i am triyng to post that

// build image
pub fn build_image_fn(docker_api_ip: &str, image_name: &str, img_file_path: &str) -> Result<String, Error> {
  let client = Client::new();
  // read the tar
  let file = File::open(img_file_path).unwrap();
  println!("path: {img_file_path}");
  // post
  let url = format!("http://{docker_api_ip}/build?t={image_name}");
  let response = client
    .post(url)
    .header("Content-Type", "application/tar")
    .body(file)
    .send();
  // Check if the request was successful
  match response {
    Ok(response) => {
      if response.status().is_success() {
        Ok("Image builded successfully".to_string())
      } else {
          let status = response.status().clone();
          let body = response.text().unwrap();
          let err = serde_json::from_str::<ApiErr>(&body).expect("failed to deserialize build_image_fn error json");
          let err_str = format!("Error: code:{}, message: {}", status, err.message);
          Ok(err_str)
      }
    }
    Err(err) => {
      Err(err)
    }
  }
}

if i do that with curl using --data-binary to send my tar, the image builds successfuly so i would like to see if there is something to replicate the behaviour of --data-binary but in rust reqwest

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
GranBoh
  • 67
  • 8
  • Is the file being mangled when uploaded or is it not showing up at all? You may need to send this to a test service you control to validate it. – tadman Jun 12 '23 at 15:26
  • Nope, i would like to see how it has to being mangled to send it to the docker api – GranBoh Jun 12 '23 at 15:30
  • You should be able to ping this off of any HTTP service that can receive an uploaded file, even some simple thing you bang together with Rust, Node, PHP or whatever. I wonder if there's some CRLF mangling going on here, or an incorrect/unexpected `Content-Type`. – tadman Jun 12 '23 at 15:31
  • Have you tried sending it as a `multipart/form-data`? with the same reqwest's feature turned on. – xamgore Jun 12 '23 at 21:42
  • You could have a look at what [`bollard`](https://docs.rs/bollard/latest/bollard/struct.Docker.html#method.build_image) does. – Caesar Jun 13 '23 at 01:17
  • Thank you @Caesar bollard worked perfectly – GranBoh Jun 13 '23 at 08:58

0 Answers0