foss42 / apidash

API Dash is a beautiful open-source cross-platform API Client built using Flutter which can help you easily create & customize your API requests, visually inspect responses and generate API integration code. A lightweight alternative to postman/insomnia.
https://apidash.dev
Apache License 2.0
1.43k stars 269 forks source link

Add Curl Rust Code Generator #342

Closed apoorvdwi closed 5 months ago

apoorvdwi commented 5 months ago

PR Description

Added curl rust code generator and corresponding test file

Related Issues

Checklist

Added/updated tests?

We encourage you to add relevant test cases.

apoorvdwi commented 5 months ago

@animator you can run the below 2 codes to verify. It will print the request headers being sent

With explicit content type set

use curl::easy::{Easy, InfoType, List};

fn main() {
    let mut easy = Easy::new();
    let mut data = Vec::new();
    easy.url("https://api.apidash.dev/io/form").unwrap();
    easy.verbose(true).unwrap();
    easy.post(true).unwrap();

    let mut form = curl::easy::Form::new();

    form.part("text").contents(b"API").add().unwrap();

    form.part("sep").contents(b"|").add().unwrap();

    form.part("times").contents(b"3").add().unwrap();

    let mut list = List::new();
    list.append("Content-Type: multipart/form-data").unwrap();
    easy.http_headers(list).unwrap();

    easy.httppost(form).unwrap();

    easy.debug_function(|info_type, data| {
        if let InfoType::HeaderOut = info_type {
            print!("Request header:\n{}", String::from_utf8_lossy(data));
        }
    })
    .unwrap();
    {
        let mut transfer = easy.transfer();
        transfer
            .write_function(|new_data| {
                data.extend_from_slice(new_data);
                Ok(new_data.len())
            })
            .unwrap();
        transfer.perform().unwrap();
    }

    let response_body = String::from_utf8_lossy(&data);

    println!("Response body: {}", response_body);
    println!("Response code: {}", easy.response_code().unwrap());
}

Without explicit content type set

use curl::easy::{Easy, InfoType};

fn main() {
    let mut easy = Easy::new();
    let mut data = Vec::new();
    easy.url("https://api.apidash.dev/io/form").unwrap();
    easy.verbose(true).unwrap();
    easy.post(true).unwrap();

    let mut form = curl::easy::Form::new();

    form.part("text").contents(b"API").add().unwrap();

    form.part("sep").contents(b"|").add().unwrap();

    form.part("times").contents(b"3").add().unwrap();

    easy.httppost(form).unwrap();

    easy.debug_function(|info_type, data| {
        if let InfoType::HeaderOut = info_type {
            print!("Request header:\n{}", String::from_utf8_lossy(data));
        }
    })
    .unwrap();
    {
        let mut transfer = easy.transfer();
        transfer
            .write_function(|new_data| {
                data.extend_from_slice(new_data);
                Ok(new_data.len())
            })
            .unwrap();
        transfer.perform().unwrap();
    }

    let response_body = String::from_utf8_lossy(&data);

    println!("Response body: {}", response_body);
    println!("Response code: {}", easy.response_code().unwrap());
}
ashitaprasad commented 5 months ago

LGTM