为了账号安全,请及时绑定邮箱和手机立即绑定

Python 请求模拟 CURL POST 发送带有 1 个或多个文件和 JSON 正文的多部分请求

Python 请求模拟 CURL POST 发送带有 1 个或多个文件和 JSON 正文的多部分请求

慕仙森 2022-06-15 16:39:44
我已经破解了两天了,没有运气!工作卷曲请求curl -X POST -v "http://$1:8080/controller/endpoint" -H "Cache-Control: no-cache" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -F "message={ \"id\": \"b3562c86-6ff4-4bf7-9c4a-4c64fff4d0ea\", \"stuff\": [{\"id\": \"1ca2d9b1-1d73-432a-b483-be404afff8da\",.......\"endTime\": \"\"}]}};type=application/json" -F "files=@file.zip"返回如下所示的输出: ./rest.sh http://127.0.0.1/anything* Hostname was NOT found in DNS cache*   Trying 127.0.0.1...* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)> POST /anything HTTP/1.1> User-Agent: curl/7.35.0> Host: 127.0.0.1> Accept: */*> Cache-Control: no-cache> Content-Length: 493> Expect: 100-continue> Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW; boundary=------------------------52912a6946761b42>< HTTP/1.1 100 Continue< HTTP/1.1 200 OK* Server gunicorn/19.9.0 is not blacklisted< Server: gunicorn/19.9.0< Date: Tue, 12 Feb 2019 18:18:56 GMT< Connection: keep-alive< Content-Type: application/json< Content-Length: 725< Access-Control-Allow-Origin: *< Access-Control-Allow-Credentials: true<{  "args": {},  "data": "",  "files": {    "files": "ZIP-CONTENT-GOES-HERE"  },  "form": {    "message": "{ \"runId\": \"1ca2d9b1-1d73-432a-b483-be404a13e8da\", \"reports\": [\n{\n\"executionId\": \"1ca2d9b1-1d73-432a-b483-be404a13e8da\",\n\"endTime\": \"\"\n}]}}"  },  "headers": {    "Accept": "*/*",    "Cache-Control": "no-cache",    "Content-Length": "493",    "Content-Type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW; boundary=------------------------52912a6946761b42",    "Expect": "100-continue",    "Host": "127.0.0.1",    "User-Agent": "curl/7.35.0"  },  "json": null,  "method": "POST",  "origin": "172.17.42.1",  "url": "http://127.0.0.1/anything"}* Connection #0 to host 127.0.0.1 left intact
查看完整描述

1 回答

?
三国纷争

TA贡献1804条经验 获得超7个赞

您的脚本应如下所示:


注意:有依赖requests_toolbelt


send.py


import argparse

import requests

from requests_toolbelt import MultipartEncoder


parser = argparse.ArgumentParser()

parser.add_argument('message')

parser.add_argument('--files', nargs='+')

args = parser.parse_args()


multipart_form_data_object = MultipartEncoder(

    fields=(

        ('files', (args.files[0], open(args.files[0], 'rb'), "application/json")),

        ('files', (args.files[1], open(args.files[1], 'rb'), "application/json")),

        ('message', ('message', open(args.message, 'rb'), 'application/json')),

    )

)


res = requests.post('http://localhost:8000', data=multipart_form_data_object, headers={'Content-Type': multipart_form_data_object.content_type})

print(res.content)

我使用 django 对其进行了测试:


urls.py


from django.urls import path

from django.http import JsonResponse

from django.views.decorators.csrf import csrf_exempt


@csrf_exempt

def dump(request):

    data = {name: [o.read().decode('utf8') for o in request.FILES.getlist(name)] for name in request.FILES.keys()}

    return JsonResponse(data)


urlpatterns = [

    path('', dump),

]

使用以下方法调用它:


curl -s http://127.0.0.1:8000/ -F "message=@$(pwd)/file1" -F "files=@$(pwd)/file2" -F "files=@$(pwd)/file3"


并使用python


python send.py file1 --files file2 file3


相同的输出:


{"files": ["{\\"message\\": \\"hello world\\"}\\n", "something else\\n"], "message": ["hello world\\n"]}



查看完整回答
反对 回复 2022-06-15
  • 1 回答
  • 0 关注
  • 326 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号