locked
Need to call the API from my code RRS feed

  • Question

  • User-1660589204 posted

    Hi,

    I am working on a console application and need to send a POST request to an API which gives me a response of ID. Please find the code below. I have an API call request code which is not working and has compile time errors.  Please help me to make an API request, get the response . Please check my code and help me with a solution.

    public void FileChecker(FileDetails fileDetails)
            {
                HttpClient client = new HttpClient();
                DirectoryInfo info = new DirectoryInfo(path);
                info.GetFiles("*.trm");
                
                DataTable FileDetailsTable = new DataTable();
                System.Net.Http.HttpResponseMessage response = client
                                            .PostAsync("https://project.au/api/v2/file/uploader", fileDetails);
                response.EnsureSuccessStatusCode();            
            }

    The compile time error is in the client.PostAsync call. The fileDetails variable shows: cannot convert from "Project.Model.FileDetails" to "System.Net.Http.HttpContent". Please help me with this.

    Monday, November 9, 2020 12:38 PM

All replies

  • User475983607 posted

    Basically, you are NOT following the reference documentation.  The PostAsync method expects an HttpContent type not FileDetials.  

    Unfortunately, you did not include the all the relevant code and explain what you're trying to do.  If you are trying to upload a file or files then your approach is incorrect.  

    https://www.google.com/search?q=HttpClient+upload+files+C%23

    Monday, November 9, 2020 12:55 PM
  • User-1660589204 posted

    Thanks mgebhard,

    I am actually sending one or more audio files available from a folder to the API. Please correct me with the right way.

    Monday, November 9, 2020 12:58 PM
  • User475983607 posted

    ddesarajubyc

    I am actually sending one or more audio files available from a folder to the API. Please correct me with the right way.

    Click the link and read through the different blogs/tutorials.  Basically, you need to create multipart form upload.  

    Monday, November 9, 2020 1:35 PM