Search code examples
pythonapache-nifi

HTTP Post Headers not arriving in nifi


I'm quite new to NiFi.

I am sending an HTTP post containing a file and some headers to Nifi, using Python:

 headers = {
     "filename": "Filename",
     "response_url": "https://...",
     "file_id": "123"}

 requests.post(NIFI_URL, files={file.filename: file}, headers=headers, verify=SSA_CA_BUNDLE)

I receive that HTTP post using a Nifi ListenHTTP processor:

enter image description here

The HTTP post appears in Nifi and I can use the file sent there, but I cannot get those HTTP headers into Nifi Attributes.

I could use the red circled attribute to tell Nifi to take those headers and make them attributes of my FlowFile, but it doesn't work.

What am I doing wrong?


Solution

  • import requests
    
    headers = {
         "filename": "Filename",
         "response_url": "https://...",
         "file_id": "123"}
    
    #the required first parameter of the 'get' method is the 'url':
    x = requests.post('http://httpbin.org/post', json={'somekey': 'somevalue'}, headers=headers)
    
    #print the response text (the content of the requested file):
    print(x.text)
    

    this will print headers python sending to server:

    {
      "args": {},
      "data": "{\"somekey\": \"somevalue\"}",
      "files": {},
      "form": {},
      "headers": {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate",
        "Content-Length": "24",
        "Content-Type": "application/json",
        "File-Id": "123",
        "Filename": "Filename",
        "Host": "httpbin.org",
        "Response-Url": "https://...",
        "User-Agent": "python-requests/2.30.0",
        "X-Amzn-Trace-Id": "Root=1-64643b3b-6f4a6aeb063667ef04d7de5e"
      },
      "json": {
        "somekey": "somevalue"
      },
      "origin": "24.218.141.179",
      "url": "http://httpbin.org/post"
    }
    

    seems python "normalizing" headers:

    "filename"      ==>  "Filename"
    "response_url"  ==>  "Response-Url"
    "file_id"       ==>  "File-Id"
    

    try to change regex to (?i)filename|response-url|file-id

    (?i) == case insensitive for java regex