2

i am trying to do a post request to upload a img to https://pasteboard.co/, but i am always getting a 500 response which tells me, there is a missing file.

The file is really existing and the path is correct, i don't know where the problem is.

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')

response = browser.open('https://pasteboard.co/')

payload = {"file": open('C:/Users/Oli/Google Drive/IMG_20190616_153432.jpg', 'rb').read()}
response = browser.post('https://pasteboard.co/upload', payload)

Its not a dublicate of: Upload Image using POST form data in Python-requests

If i try the same code like there:

import requests
session = requests.Session()
headers = {
    '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'}
session.headers = headers


session.get('https://pasteboard.co/')

image_file_descriptor = open('C:/Users/Oli/Google Drive/IMG_20190616_153432.jpg', 'rb').read()
payload = {"file": image_file_descriptor}

a = requests.post('https://pasteboard.co/upload', files=payload, headers=headers)

I get a 502 Bad Gateway error.

Oliver W
  • 45
  • 1
  • 8
  • 1
    Possible duplicate of [Upload Image using POST form data in Python-requests](https://stackoverflow.com/questions/29104107/upload-image-using-post-form-data-in-python-requests) – asikorski Jul 15 '19 at 10:33
  • I think its not a dublicate, i updatet my question – Oliver W Jul 15 '19 at 11:10
  • Perhaps try base64 encoding it, it could potentially be invalid characters – Peter Jul 15 '19 at 11:49

1 Answers1

1

I made it using requests module Try this code:

import requests
import json

header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}
img_file = open(r'C:/Users/Oli/Google Drive/IMG_20190616_153432.jpg', 'rb')
header['Content-Type'] = 'multipart/form-data'
files = {'file': ('Image.jpg', img_file, 'image/jpeg', {'Expires': '10'}) }
res = requests.post('https://pasteboard.co/upload', files=files)
uploaded_image_name = json.loads(res.content.decode('utf-8'))['fileName']
print(f'New Link: https://pasteboard.co/{uploaded_image_name}')

if you upload a png just change the following things:

1.Firstly,

files = {'file': ('Image.png', img_file, 'image/png', {'Expires': '10'}) }

2. Path to the Image.

Check if this works for you.

Nitin
  • 246
  • 1
  • 7
  • Well thank you, this is a smooth solution. But where did you know, you have to add: ('Image.jpg', img_file, 'image/jpeg')? And i thought i could solve another problem, if i solve to upload this image to this hoster, but i don't get it. Could you maybe check out my other topic and please help me? https://stackoverflow.com/questions/57028388/mechanize-uploading-image-returns-403-error – Oliver W Jul 15 '19 at 16:46
  • Read about it in the documentation of requests module [HERE](https://2.python-requests.org/en/master/) – Nitin Jul 15 '19 at 18:14