title: ASP.NET Web API HTTPRequestMessage with CLR Object to upload file
body: <p>I want to upload file to server using ASP.NET Web API with Security token in header and embed CLR object in the <code>HTTPRequestMessage</code>.</p>
  
  <p>I can find solution to POST file using Web API Async with multipart content type but there is no solution to upload with custom HTTP request message with header token...</p>
  
  <p>I am using the below code</p>
  
  <pre><code>    public HttpResponseMessage UploadImportFile()
      {
          List&lt;string&gt; FileIdentifiers = new List&lt;string&gt;();
  
          // Check if the request contains multipart/form-data.
          if (!Request.Content.IsMimeMultipartContent())
          {
              throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
          }
  
          string root = ImportFileDirectory;
          MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(root);
  
          try
          {
              // Read the form data.
              Request.Content.ReadAsMultipartAsync(provider);
  
              foreach (MultipartFileData file in provider.FileData)
              {
                  FileIdentifiers.Add(file.LocalFileName.Replace(root, "").Replace("BodyPart_", ""));
              }
              return Request.CreateResponse(HttpStatusCode.OK, new FileUploadResponseType(Ackowledgememt.Success, UtcDateTime.Now, null, version, FileIdentifiers));
          }
          catch (System.Exception e)
          {
              log.Error(e);
              return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
          }
      }
  </code></pre>