Skip to content
Advertisement

Rust: View entire Command::New command arguement structs as its passed to terminal / correct my arguements

I am not getting the correct response from the server running the following

Command::new("curl")
       .args(&["-X",
        "POST",
        "--header",
        &("Authorization: Bearer ".to_owned() + &return_token("auth")),
        "--header",
        "Content-Type: application/json",
        "-d",
        parameters,
        "https://api.tdameritrade.com/v1/accounts/CORRECT_ACCOUNT_#/orders",
    ])
    .output()
    .unwrap()
    .stdout;

Command::new("curl")
         .arg("-X")
         .arg("POST")
         .arg("--header")
         .arg("Authorization: Bearer ".to_owned() + &return_token("auth"))
         .arg("--header")
         .arg("Content-Type: application/json")
         .arg("-d")
         .arg(parameters)
         .arg("https://api.tdameritrade.com/v1/accounts/CORRECT_ACCOUNT_#/orders")
    .output()
    .unwrap()
    .stdout;

However…. the following works fine if I run it in terminal. I form the following using let line = "curl -X POST --header "Authorization: Bearer ".to_owned() + &return_token("auth") + "" --header "Content-Type: application/json" -d " + parameters + " "https://api.tdameritrade.com/v1/accounts/CORRECT_ACCOUNT_#/orders"";

curl -X POST --header "Authorization: Bearer LONG_RANDOM_AUTH_TOKEN" --header "Content-Type: application/json" -d "{
"complexOrderStrategyType": "NONE",
"orderType": "LIMIT",
"session": "NORMAL",
"price": "0.01",
"duration": "DAY",
"orderStrategyType": "SINGLE",
"orderLegCollection": [
  {
    "instruction": "BUY_TO_OPEN",
    "quantity": 1,
    "instrument": {
      "symbol": "SPY_041621P190",
      "assetType": "OPTION"
  }
  }
]
}" "https://api.tdameritrade.com/v1/accounts/CORRECT_ACCOUNT_#/orders"

“parameters” can be seen above in JSON.

How can do I view the formation of the command that Command::New is making or correct my args?

EDIT Ive tried using a single quote around the JSON and not escaping the double quotes, which also works in the terminal.

EDIT Example included. I found this : https://github.com/rust-lang/rust/issues/29494 && https://users.rust-lang.org/t/std-process-is-escaping-a-raw-string-literal-when-i-dont-want-it-to/19441/14

However Im in linux…

fn main() {

// None of the following work

let parameters = r#"{"complexOrderStrategyType":"NONE","orderType":"LIMIT","session":"NORMAL","price":"0.01","duration":"DAY","orderStrategyType":"SINGLE","orderLegCollection":[{"instruction":"BUY_TO_OPEN","quantity":1,"instrument":{"symbol":"SPY_041621P190","assetType":"OPTION"}}]}"#;

// OR

let parameters = "'{"complexOrderStrategyType":"NONE","orderType": "LIMIT","session": "NORMAL","price": "0.01","duration": "DAY","orderStrategyType": "SINGLE","orderLegCollection": [{"instruction": "BUY_TO_OPEN","quantity": 1,"instrument": {"symbol": "SPY_041621P190","assetType": "OPTION"}}]}'";

// OR 

let parameters = "{"complexOrderStrategyType":"NONE","orderType": "LIMIT","session": "NORMAL","price": "0.01","duration": "DAY","orderStrategyType": "SINGLE","orderLegCollection": [{"instruction": "BUY_TO_OPEN","quantity": 1,"instrument": {"symbol": "SPY_041621P190","assetType": "OPTION"}}]}";

// OR 

let parameters = "{'complexOrderStrategyType':'NONE','orderType': 'LIMIT','session': 'NORMAL','price': '0.01','duration': 'DAY','orderStrategyType': 'SINGLE','orderLegCollection': [{'instruction': 'BUY_TO_OPEN','quantity': 1,'instrument': {'symbol': 'SPY_041621P190','assetType': 'OPTION'}}]}";

    println!("{:?}", str::from_utf8(&curl(parameters, "ORDER")).unwrap());

    fn curl(parameters: &str, request_type: &str) -> Vec<u8> {
        let mut output = Vec::new();

        if request_type == "ORDER" {
            output = Command::new("curl")
                .args(&[
                    "-X",
                    "POST",
                    "--header",
                    "Authorization: Bearer AUTH_KEY_NOT_INCLUDED",
                    "--header",
                    "Content-Type: application/json",
                    "-d",
                    parameters,
                    "https://api.tdameritrade.com/v1/accounts/ACCOUNT_NUMBER_NOT_INCLUDED/orders",
                ])
                .output()
                .unwrap()
                .stdout;
        }
        output
    }
}

Advertisement

Answer

For reasons unknown… changing the –header switch / flag to -H solved the problem. As shown in the following. Spoke with a friend and apparently the shortened form may take different parameters.

let parameters = "{
"complexOrderStrategyType": "NONE",
"orderType": "LIMIT",
"session": "NORMAL",
"price": "0.01",
"duration": "DAY",
"orderStrategyType": "SINGLE",
"orderLegCollection": [
  {
    "instruction": "BUY_TO_OPEN",
    "quantity": 1,
    "instrument": {
      "symbol": "SPY_041621P190",
      "assetType": "OPTION"
  }
  }
]
}";

 Command::new("curl")
    .args(&["-X",
    "POST",
    "-H",
    &("Authorization: Bearer ".to_owned() + &return_token("auth")),
    "-H",
    "Content-Type: application/json",
    "-d",
    parameters,
    "https://api.tdameritrade.com/v1/accounts/ACCOUNT/orders",
])
.output()
.unwrap()
.stdout;
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement