
[code]

    POST /v2/users/create HTTP/1.1
    Host: mixch.tv
    Connection: keep-alive
    X-Mixch-Country: jp
    X-Mixch-Attribution: Organic
    Accept: */*
    X-Mixch-AdvertisingId: A29428C7-8C02-419F-A071-E47E3DA184A9
    User-Agent: mixch/5.2.1 (iPhone; iOS 10.2; Scale/2.00)
    Accept-Language: ja-JP;q=1
    Accept-Encoding: gzip, deflate
    Content-Length: 262033
    Content-Type: multipart/form-data; boundary=Boundary+EF9D031E6AE26D0E
    
    --Boundary+EF9D031E6AE26D0E
    Content-Disposition: form-data; name="birth_year"
    
    -1
    --Boundary+EF9D031E6AE26D0E
    Content-Disposition: form-data; name="nickname"
    
    Hi
    --Boundary+EF9D031E6AE26D0E
    Content-Disposition: form-data; name="prefecture"
    
    -1
    --Boundary+EF9D031E6AE26D0E
    Content-Disposition: form-data; name="file"; filename="file.png"
    Content-Type: image/jpeg
    
    ����
    
[/code]

これのmultipartのリクエストの送信の仕方を教えてください...  
Python2でお願いします。


CC BY-SA 3.0
2017-09-03T10:16:10.987
37696
2017-09-05T12:06:49.067
2017-09-05T12:06:49.067
20532
22925
question
python
multipartのrequestsについてなのです


マニュアルにそのまんまのmultipartの例がありますよ。  
<http://requests.readthedocs.io/en/latest/user/quickstart/#post-a-multipart-
encoded-file>

[code]

    import requests
    
    
    data = dict(
        birth_year=-1,
        nickname="Hi",
        prefecture=-1,
    )
    
    files = dict(
        file=open('xxx.png', 'rb')
    )
    
    r = requests.post('http://localhost:8888', files=files, data=data)
    print(r.text)
    
[/code]

別のターミナルで`nc -l
8888`として実行すると、次の内容がPOSTされていることがわかります。BoundaryのIDは実行時に決まるランダムな文字列なので、値をきにする必要はありません。

[code]

    $ nc -l 8888
    POST / HTTP/1.1
    Host: localhost:8888
    User-Agent: python-requests/2.18.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    Content-Length: 420
    Content-Type: multipart/form-data; boundary=177b060d764643c398b667b5841c06df
    
    --177b060d764643c398b667b5841c06df
    Content-Disposition: form-data; name="birth_year"
    
    -1
    --177b060d764643c398b667b5841c06df
    Content-Disposition: form-data; name="nickname"
    
    Hi
    --177b060d764643c398b667b5841c06df
    Content-Disposition: form-data; name="prefecture"
    
    -1
    --177b060d764643c398b667b5841c06df
    Content-Disposition: form-data; name="file"; filename="xxx.png"
    
    
    --177b060d764643c398b667b5841c06df--
    
[/code]


CC BY-SA 3.0
2017-09-03T17:30:21.220
37700
2017-09-03T17:30:21.220
7837
37696
answer