Home > Software engineering >  .Net Framework API endpoint for file upload: Cannot make a request with Postman
.Net Framework API endpoint for file upload: Cannot make a request with Postman

Time:03-29

I'm creating a .Net framework (not DotNet Core) API endpoint to upload a file, and I'm not being able to debug the request in Visual Studio requesting from Postman. My app uses Swagger, and I can do a request with Swagger and debug with no problem, except that with Swagger I'm not able to display a "Select File" button, so I'm trying to use Postman.

My method, even though it's not finished yet, looks like this:

[HttpPost]
[Route("UploadVideo")]
public bool UploadVideo([FromBody] JObject pData, [FromUri] string videoTitle, [FromUri] string description,
                          [FromUri] short enterpriseId, [FromUri] int userId, [FromUri] int applicationId)
{
       string lFileBase64 = pData["file"]["File64Code"].ToString();
       string lFileName = pData["file"]["FileName"].ToString();
            Dictionary<string, string> exportParams = new Dictionary<string, string>();
       exportParams.Add("TYPE", ((int)RequestType.UsersImport).ToString());
       exportParams.Add("PFK_ENTERPRISE", enterpriseId.ToString());
       exportParams.Add("PK_USER", userId.ToString());
       exportParams.Add("PK_APPLICATION", applicationId.ToString());
       exportParams.Add("FILE_NAME", lFileName);

       Models.RequestLog requestLog = new Models.RequestLog();
       requestLog.Data = RequestLogService.ParseRequestParams(exportParams);
       requestLog.Type = RequestType.UsersImport;

       int requestId = RequestLogService.CreateRequestLog(enterpriseId, userId,
                 Mapper.Map<RequestLog>(requestLog));

       if (requestId > 0)
       {
         //update request status
         exportParams.Add("PK_REQUEST", requestId.ToString());
         requestLog.Id = requestId;
         requestLog.Status = RequestStatus.Sent;
         requestLog.Data = RequestLogService.ParseRequestParams(exportParams); ;
         RequestLogService.UpdateRequestLog(enterpriseId, userId,
                    Mapper.Map<RequestLog>(requestLog));

         string pCloudStorageAccount = AzureHelper.SelectAzureStorageAccount(enterpriseId);
         string pContainer = "docstmp"   AzureStorageSettings.AzureStorageContainer;
         MemoryStream myStream = new MemoryStream(Convert.FromBase64String(lFileBase64));
         Guid guid = Guid.NewGuid();
         lFileName = "UploadVideo_"   enterpriseId.ToString()   "_"   guid.ToString()   ".mp4";
         string urlAzure = AzureStorageHelpers.UploadFile(pCloudStorageAccount, pContainer, lFileName, myStream);
         exportParams.Add("URL", urlAzure);

         requestLog.Data = RequestLogService.ParseRequestParams(exportParams);
         RequestLogService.QueueRequest(enterpriseId, userId,
             Mapper.Map<RequestLog>(requestLog));
       }
       else
       {
         return false;
       }

       return true;
}

But the method is not relevant here, as my problem is Postman is not reaching the endpoint.

In the following pictures you can see details of the Postman request with the error thrown.

Headers

Request

Any help?

Edit 1: Extra info get from Swagger that might be of help.

"/api/Catalogues/UploadVideo":{
   "post":{
      "tags":[
         "Catalogues"
      ],
      "operationId":"Catalogues_UploadVideo",
      "consumes":[
         "application/json",
         "text/json",
         "application/x-www-form-urlencoded"
      ],
      "produces":[
         "application/json",
         "text/json"
      ],
      "parameters":[
         {
            "name":"pData",
            "in":"body",
            "required":true,
            "schema":{
               "type":"object"
            }
         },
         {
            "name":"videoTitle",
            "in":"query",
            "required":true,
            "type":"string"
         },
         {
            "name":"description",
            "in":"query",
            "required":true,
            "type":"string"
         },
         {
            "name":"enterpriseId",
            "in":"query",
            "required":true,
            "type":"integer",
            "format":"int32"
         },
         {
            "name":"userId",
            "in":"query",
            "required":true,
            "type":"integer",
            "format":"int32"
         },
         {
            "name":"applicationId",
            "in":"query",
            "required":true,
            "type":"integer",
            "format":"int32"
         }
      ],
      "responses":{
         "200":{
            "description":"OK",
            "schema":{
               "type":"boolean"
            }
         }
      }
   }
}

Edit 2: Already disabled SSL Certificate verification in Postman:

Disabled SSL Verification in Postman

CodePudding user response:

In your postman, you are sending all data using a form (body). Your endpoint is expecting all values, except the video, to be sent via the URL as query parameters.

In postman, add all your values to the Params tab, and add the video to the Body tab as you've done.

The attributes on the properties on your endpoint, you've stated this. [FromBody] means the form being sent with the POST. [FromUri] tells the endpoint it is expecting a value to be included in the URL itself.

This is also outlined in your swagger example:

Body

"name":"pData",
"in":"body" //<--this

Query

"name":"videoTitle",
"in":"query", //<--this

EDIT: I suggest following this MS document. Use the correct methodology for uploading large files.

  • Related