Search code examples
c#jsonpostmanrestsharp

Connect to a Rest API, using a postman collection


I'm trying to connect to an API that I have a Postman Collection json:

{
    "info": {
        "_postman_id": "----",
        "name": "Public API",
        "description": "API",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
"item": [
        {
            "name": "Add Bim Info for a building",
            "request": {
                "auth": {
                    "type": "basic",
                    "basic": [
                        {
                            "key": "password",
                            "value": "somepassword",
                            "type": "string"
                        },
                        {
                            "key": "username",
                            "value": "someusername",
                            "type": "string"
                        }
                    ]
                },
                "method": "POST",
                "header": [],
                "body": {
                    "mode": "formdata",
                    "formdata": [
                        {
                            "key": "file",
                            "type": "file",
                            "src": []
                        }
                    ]
                },
                "url": {
                    "raw": "http://00.00.00.00:0000/api/apiName/building/",
                    "protocol": "http",
                    "host": [
                        "00",
                        "00",
                        "00",
                        "00"
                    ],
                    "port": "0000",
                    "path": [
                        "api",
                        "apiName",
                        "building",
                        ""
                    ]
                },
                "description": "description"
            },
            "response": []
        }
]

When I load this json to postman I can send files without problem, but I have to implement a console program to do it, so I've tried to translate the postman collection to c# code using RestSharp:

    string filePath = @"path_to_file";
    string url = "http://00.00.00.00:0000/api/apiName/building/";
    string usr = "someuser";
    string pwd = "somepassword";

    RestClient client = new 
    RestClient(url);
    client.Authenticator = new HttpBasicAuthenticator(usr, pwd);

    RestRequest request = new RestRequest(url);
    request.Method = Method.POST;
    request.AddFile(Path.GetFileNameWithoutExtension(filePath), filePath);

    try
    {
        IRestResponse response = client.Execute(request);
        Console.WriteLine(response.Content);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
    Console.ReadLine();

The Authenticator works fine, but when I send the request I get the message: "Required request part 'file' is not present"

How can I add this request? Can I use the json as some template to send my request?


Solution

  • The question has been answered thanks to the comment of @DavidG, I write the answer to make it oficial.

    RestRequest request = new RestRequest(url);
    
    request.Method = Method.POST;
    request.AddFile("file", filePath); //Changing this line generates the body in the json