1

I would like to add content type explicity for multipart form data before sending post request Below is my sample code i managed to add conten type for file data but couldn't figure out how to add content type correctly for json data, i would like to add "application/json; charset=utf-8" for json data

import requests
import json
import traceback

def uploadLogs(fileName):
    f = open(fileName, 'rb')
    payload = { "var1":"this", "var2" : "that"
    }
    files = {'file': ('current', f, "text/plain; charset=us-ascii")}
    data = {'info': json.dumps(payload)}
    headers = {'type': 'myReport', "Keep-Alive": "timeout=100"}

    try:
        url = "http://localhost:8009/upload"
        response = requests.post(url, data=data, files=files, headers=headers)
        print(response.request.body)
        print(response.request.headers)
        print(response.status_code)
        if (response != None and (response.status_code == 200 or
                                  response.status_code == 201)):
            return True
    except:
        traceback.print_exc()

    return False

filename = "C:\\sample.txt"
print(uploadLogs(filename))

If someone knows how to do please suggest

Sahil Saxena
  • 131
  • 4
  • 11
  • is it `{'Content-type': 'application/json'}` in the header? [source - will be dup target if that's all you need](https://stackoverflow.com/questions/32651362/how-do-i-set-the-content-type-for-post-requests-in-python-requests-library) – Tadhg McDonald-Jensen Nov 04 '20 at 17:00
  • 2
    as part of form-data , similar to files – Sahil Saxena Nov 04 '20 at 17:03

0 Answers0