0

I want to send a multipart/form-data which contains a image with python requests.

I already tried something this but it was not working properly can any suggest me something ?

import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder

print "Hello"

url = "https://api.testdomain.com/api/3.1/listings/"

token = 'my-token'
headers = {
    'authorization': "Bearer " + token,
    'Content-Type': 'multipart/form-data; boundary=mybound',
    'platform': 'ios'
}

multipart_data = MultipartEncoder(
    fields={
            # a file upload field
            'photo_0': ('pic.jpg', open('pic.jpg', 'rb'), 'image/jpeg','image_0.jpg'),
            # plain text fields
            'shipping_sg_other_name': 'atikrahman', 
            'collection_id': '2239',
            'description': 'Test bag descriptions 5',
            'material_type': '',
            'location_latlon': '999.0,999.0',
            'photo_0_hash': '',
            'multi_quantities': 'true',
            'shipping_sg_normal_options': 'false',
            'price': '60',
            'abcpay': 'false',
            'abcpay_sg_choices': 'atikrahman',
            'condition': '2',
            'shipping_sg_other_fee': '3.0',
            'meetup': 'false',
            'shipping_sg_smartpac_options': 'false',
            'is_mobile_verification_listing_flow_enabled': 'true',
            'shipping_sg_other': 'true',
            'title': 'TES bag 5',
            'shipping_sg_other_details': '',
            'detect_phone_number': 'false',
            'brand': '',
            'mailing': 'true',
            'shipping_sg_registered_options': 'false',
           }
    )

response = requests.request("POST", url,data=multipart_data,headers=headers)
print(response.text)

My HTTP REQUEST looks something like this

I want to make a python request of this http request. I new in python that's why having some problem.

POST /api/3.1/listings/ HTTP/1.1
Host: api.testdomain.com
Authorization: Bearer my-token
X-Client-Version: 2.103.0
Accept: */*
Accept-Language: en-CN;q=1.0, zh-Hans-CN;q=0.9
Accept-Encoding: gzip, deflate
platform: ios
Content-Length: 1913
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 12_0_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16A404
build-no: 958
Connection: close
Content-Type: multipart/form-data; boundary=mybound
Cookie: __cfduid=dd4186742eb52fc594d39235a3870e1c11552535680

--mybound
Content-Type: image/jpeg
Content-Disposition: form-data; name="photo_0"; filename="image_0.jpg"

[image_source_code]

--mybound
Content-Disposition: form-data; name="shipping_sg_other_name"

atikrahman
--mybound
Content-Disposition: form-data; name="collection_id"

2239
--mybound
Content-Disposition: form-data; name="description"

Test bag descriptions 6
--mybound
Content-Disposition: form-data; name="material_type"


--mybound
Content-Disposition: form-data; name="location_latlon"

999.0,999.0
--mybound
Content-Disposition: form-data; name="photo_0_hash"


--mybound
Content-Disposition: form-data; name="multi_quantities"

true
--mybound
Content-Disposition: form-data; name="shipping_sg_normal_options"

false
--mybound
Content-Disposition: form-data; name="price"

32.0
--mybound
Content-Disposition: form-data; name="abcpay"

false
--mybound
Content-Disposition: form-data; name="abcpay_sg_choices"

Wildfemale 
--mybound
Content-Disposition: form-data; name="condition"

2
--mybound
Content-Disposition: form-data; name="shipping_sg_other_fee"

3.0
--mybound
Content-Disposition: form-data; name="meetup"

false
--mybound
Content-Disposition: form-data; name="shipping_sg_smartpac_options"

false
--mybound
Content-Disposition: form-data; name="is_mobile_verification_listing_flow_enabled"

true
--mybound
Content-Disposition: form-data; name="shipping_sg_other"

true
--mybound
Content-Disposition: form-data; name="title"

Test bag 7
--mybound
Content-Disposition: form-data; name="shipping_sg_other_details"


--mybound
Content-Disposition: form-data; name="detect_phone_number"

false
--mybound
Content-Disposition: form-data; name="brand"


--mybound
Content-Disposition: form-data; name="mailing"

true
--mybound
Content-Disposition: form-data; name="shipping_sg_registered_options"

false
--mybound--

Thanks

t.m.adam
  • 15,106
  • 3
  • 32
  • 52
Atik Rahman
  • 81
  • 2
  • 13
  • What's the error? – Alderven Mar 15 '19 at 13:40
  • Relevant [how-to-send-a-multipart-form-data-with-requests-in-python/35974071](https://stackoverflow.com/questions/12385179/how-to-send-a-multipart-form-data-with-requests-in-python/35974071#35974071) – stovfl Mar 15 '19 at 13:48
  • `dictionary update sequence element #0 has length 1 2 is required python` I fetching this error – Atik Rahman Mar 15 '19 at 14:45
  • I've edited your post because it seems your Bearer token contains information about your account. Note that this information will remain in the edits history, but it can be removed if you delete your question. – t.m.adam Mar 16 '19 at 19:30

1 Answers1

1

You probably don't need all that much.

Pass the data and files as arguments. The multipart request will be constructed automatically.

See the docs.

import requests

# ...

data = {
    'shipping_sg_other_name': 'atikrahman',
    # .... 
    'shipping_sg_registered_options': 'false',
}

files = ['/path/to/pic.jpg']

response = requests.post(url, data=data, files=files, headers=headers)
print(response.text)
  • Note that your code throws a `ValueError` exception (with Python v3.7, Requests v2.21.0) because the `files` parameter expects a dictionary, or other iterable that contains pairs of values. – t.m.adam Mar 16 '19 at 20:10