-2

I have a Blob trigger and in that a stream object is there. I want to pass the content/text to a httpclinet post call. How can i do that?

I am having a csv file with 3 lines of data each column is separated by comma

Content in my CSV :

File1,121,12123,5676,fgfg,gfg,fg,
File2,ertet,etert,ert,
File3,354345,345345
[FunctionName("Function1")]
public static void Run([BlobTrigger("expenses/{name}.csv", Connection = "AzureWebJobsStorage")]Stream inputBlob, string name,
    [Table("Expenses", Connection = "AzureWebJobsStorage")] IAsyncCollector<Expense> expenseTable,
    ILogger log)
{

Here want to convert the stream inputBlob to string/string builder and want to get the result same as the data which is there in the file.

After that I want to call an (PostAsync call) API with content-type : text/csv and the data mentioned above. I read about httplient. If I am using HttpClient how do I pass the content-type and the req body

jubi
  • 569
  • 1
  • 10
  • 34

1 Answers1

0

For your first question, you can use StreamReader, code snippet like below:

    [FunctionName("Function1")]
    public static void Run([BlobTrigger("expenses/{name}.csv", Connection = "AzureWebJobsStorage")]Stream inputBlob, string name,
    [Table("Expenses", Connection = "AzureWebJobsStorage")] IAsyncCollector<Expense> expenseTable,
    ILogger log)
    {

        //your other code

        using (StreamReader reader = new StreamReader(myBlob))
        {
            string my_content = reader.ReadToEnd();

        }
    }

For your 2nd question, when you use HttpClient, then add the following code to your function:

  [FunctionName("Function1")]
    public static void Run([BlobTrigger("expenses/{name}.csv", Connection = "AzureWebJobsStorage")]Stream inputBlob, string name,
    [Table("Expenses", Connection = "AzureWebJobsStorage")] IAsyncCollector<Expense> expenseTable,
    ILogger log)
    {

     //your other code.

     //here, use the HttpClient

     //define the http verb
     var method = new HttpMethod("POST"); 

     //add the content to body
     var request = new HttpRequestMessage(method, requestUri)
      {
            Content = new StringContent("your string to post")
      };

      //add content type
      request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/csv"));

      //send
      var httpClient = new HttpClient();
      var result = httpClient.SendAsync(request).Result;

    }
Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
  • How do i add boundary in this request – jubi Oct 11 '19 at 08:57
  • @jubi, take a look at this [answer](https://stackoverflow.com/questions/18500040/httpclient-setting-boundary-with-content-type?answertab=active#tab-top) – Ivan Glasenberg Oct 11 '19 at 09:12
  • I would like to follow your code. how do i include the boundary in ur code – jubi Oct 11 '19 at 09:15
  • My question is should i remover equest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/csv")); – jubi Oct 11 '19 at 09:17
  • @jubi, I just did try using httpClient without boundary(since I'm not familiar with this boundary header). can you please take a try at your side? – Ivan Glasenberg Oct 14 '19 at 05:32