Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

ListenHTTP processor

avatar
Expert Contributor

Hello

I want to send a file over http to nifi. When i do so, the filename attributes is set to a long sequence of numbers as shown below in the picture. How should my http url be?

listenhttpflowfile.jpg

this is the code im working with

Code Sample as below:

  public string PutNifi(string filePath)
        {
            // Create a http request to the server endpoint that will pick up the
            // file and file description.
            string Message = "Failed";
            try
            {
                HttpWebRequest requestToServerEndpoint = (HttpWebRequest)WebRequest.Create("http://db-hdf:8081/contentListener");


                string boundaryString = "----SomeRandomText";
                string fileUrl = filePath;


                // Set the http request header \\
                requestToServerEndpoint.Method = WebRequestMethods.Http.Post;
                requestToServerEndpoint.ContentType = "multipart/form-data; boundary=" + boundaryString;
                requestToServerEndpoint.KeepAlive = true;
                requestToServerEndpoint.Credentials = System.Net.CredentialCache.DefaultCredentials;


                // Use a MemoryStream to form the post data request,
                // so that we can get the content-length attribute.
                MemoryStream postDataStream = new MemoryStream();
                StreamWriter postDataWriter = new StreamWriter(postDataStream);


                // Include the file in the post data
                postDataWriter.Write("\r\n--" + boundaryString + "\r\n");
                postDataWriter.Write("Content-Disposition: form-data;"
                                        + "name=\"{0}\";"
                                        + "filename=\"{1}\""
                                        + "\r\nContent-Type: {2}\r\n\r\n",
                                        "myFile",
                                        Path.GetFileName(fileUrl),
                                        Path.GetExtension(fileUrl));
                postDataWriter.Flush();


                // Read the file
                FileStream fileStream = new FileStream(fileUrl, FileMode.Open, FileAccess.Read);
                byte[] buffer = new byte[1024];
                int bytesRead = 0;
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    postDataStream.Write(buffer, 0, bytesRead);
                }
                fileStream.Close();


                postDataWriter.Write("\r\n--" + boundaryString + "--\r\n");
                postDataWriter.Flush();


                // Set the http request body content length
                requestToServerEndpoint.ContentLength = postDataStream.Length;


                // Dump the post data from the memory stream to the request stream
                using (Stream s = requestToServerEndpoint.GetRequestStream())
                {
                    postDataStream.WriteTo(s);
                }
                postDataStream.Close();
                Message = "Success";
            }
            catch (Exception ee)
            {
                Message = ee.ToString();
            }
            return Message;
        }
    }
1 ACCEPTED SOLUTION

avatar
Master Guru

If you send a header named "filename" it should respect that value for the filename attribute in NiFi:

https://github.com/apache/nifi/blob/master/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-proce...

Basically HTTP headers become FlowFile attributes, and the body of the HTTP request becomes FlowFile content.

Write now it looks like you are writing the filename into the body of the POST request.

View solution in original post

2 REPLIES 2

avatar
Master Guru

If you send a header named "filename" it should respect that value for the filename attribute in NiFi:

https://github.com/apache/nifi/blob/master/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-proce...

Basically HTTP headers become FlowFile attributes, and the body of the HTTP request becomes FlowFile content.

Write now it looks like you are writing the filename into the body of the POST request.

avatar
Expert Contributor

thank you it worked