title: Send file with a json request
body: <p>i am trying to send to a webpage a file with a textmessage, but i have a problem with sending the file to the server. The textmessage isn't a problem</p>
  
  <p>Update extra code with login to test it.</p>
  
  <pre><code>#!/usr/bin/env python
  # -*- coding: utf-8 -*-
  from bs4 import BeautifulSoup
  import time
  import mechanicalsoup
  
  browser = mechanicalsoup.StatefulBrowser()
  browser.set_user_agent(
      'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36')
  
  base_url = 'https://www.ebay-kleinanzeigen.de/'
  
  
  def login():
      a = browser.open('https://www.ebay-kleinanzeigen.de/m-einloggen.html')
      time.sleep(2)
      browser.select_form('#login-form')
      browser['loginMail'] = 'testaccount@8.dnsabr.com'
      browser['password'] = 'testaccount'
      csrf_token = a.soup.find('input', {'name': '_csrf'})['value']
      print('crsf', csrf_token)
      browser.submit_selected()
  
      response = str(browser.get_current_page())
      soup = BeautifulSoup(response, 'html.parser')
  
      if 'angemeldet als:' in str(soup.find('span', {'id': 'user-email'})):
          print('Erfolgreich eingeloggt!')
          return csrf_token
      else:
          a = soup.find('div', {'class': 'outcomebox-warning l-container-row'})
          a = a.findNext('h2')
          print('ERROR:', a.contents[0])
          return False
  
  
  crsf = login()
  
  
  
  headers = {
      "accept": "application/json, text/javascript, */*; q=0.01",
      "accept-encoding": "gzip, deflate, br",
      "accept-language": "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7",
      "content-type": "multipart/form-data",
      "x-csrf-token": crsf,
      "x-requested-with": "XMLHttpRequest",
      "origin": "https://www.ebay-kleinanzeigen.de",
      "referer": "https://www.ebay-kleinanzeigen.de/m-nachrichten.html"
  }
  
  img_file = open('img.jpg', 'rb')
  
  file = {'contents': img_file, 'filename': 'name.jpg', 'format': 'image/jpeg'}
  
  payload = {
      'id': "8td:32br0sx:2ck88rzjt",
      'message': 'message'
  }
  
  res = browser.post('https://www.ebay-kleinanzeigen.de/m-nachricht-schreiben.json', json=payload, headers=headers, files=file)
  </code></pre>
  
  <p>I am getting only a 500 response code, the message and the image file aren't send.</p>
  
  <p>Here is the post request from the browser:
  <a href="https://i.stack.imgur.com/sHIHW.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sHIHW.jpg" alt="postdata"></a></p>