python将一个字典的内容添加到另一个字典_python将字典的文字值分配给另一个字典的键...

I am trying to form a web payload for a particular request body but unable to get it right. What I need is to pass my body data as below

data={'file-data':{"key1": "3","key2": "6","key3": "8"}}

My complete payload request looks like this

payload={url,headers, data={'file-data':{"key1": "3","key2": "6","key3": "8"}},files=files}

However, when I pass this, python tries to parse each individual key value and assigns to the 'file-data' key like this

file-data=key1

file-data=key2

file-data=key3

and so on for as many keys I pass within the nested dictionary. The requirement however, is to pass the entire dictionary as a literal content like this(without splitting the values by each key):

file-data={"key1": "3","key2": "6","key3": "8"}

The intended HTTP trace should thus ideally look like this:

POST /sample_URL/ HTTP/1.1

Host: sample_host.com

Authorization: Basic XYZ=

Cache-Control: no-cache

Content-Type: multipart/form-data; boundary=----UVWXXXX

------WebKitFormBoundaryXYZ

Content-Disposition: form-data; name="file-data"

{"key1": "3","key2": "6","key3":"8" }

------WebKitFormBoundaryMANZXC

Content-Disposition: form-data; name="file"; filename=""

Content-Type:

------WebKitFormBoundaryBNM--

As such, I want to use this as part of a payload for a POST request(using python requests library). Any suggestions are appreciated in advance-

Edit1: To provide more clarity, the API definition is this:

Body

Type: multipart/form-data

Form Parameters

file: required (file)

The file to be uploaded

file-data: (string)

Example:

{

"key1": "3",

"key2": "6",

"key3": "8"

}

The python code snippet I used(after checking suggestions) is this:

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import requests

url = "https://sample_url/upload"

filepath='mypath'

filename='logo.png'

f=open(filepath+'\\'+filename)

filedata={'file-data':"{'key1': '3','key2': '6','key3': '8'}"}

base64string = encodestring('%s:%s' % ('user', 'password').replace('\n', '')

headers={'Content-type': 'multipart/form-data','Authorization':'Basic %s' % base64string}

r = requests.post(url=url,headers=headers,data=filedata,files={'file':f})

print r.text

The error I get now is still the same as shown below:

{"statusCode":400,"errorMessages":[{"severity":"ERROR","errorMessage":"An exception has occurred"]

It also says that some entries are either missing or incorrect. Note that I have tried passing the file parameter after opening it in binary mode as well but it throws the same error message

I got the HTTP trace printed out via python too and it looks like this:

send: 'POST sample_url HTTP/1.1

Host: abc.com

Connection: keep-alive

Accept-Encoding: gzip,deflate

Accept: */*

python-requests/2.11.1

Content-type: multipart/form-data

Authorization: Basic ABCDXXX=

Content-Length: 342

--CDXXXXYYYYY

Content-Disposition:form-data; name="file-data"

{\'key1\': \'3\',\'key2\': \'6\'

,\'key3\': \'8\'}

--88cdLMNO999999

Content-Disposition: form-data; name="file";

filename="logo.png"\x89PNG\n\r\n--cbCDEXXXNNNN--

解决方案

If you want to post JSON with python requests, you should NOT use data but json:

r = requests.post('http://httpbin.org/post', json={"key": "value"})

I can only guess that you are using data because of your example

payload={url,headers, data={'file-data':{"key1": "3","key2": "6","key3": "8"}},files=files}

Whis is not valid python syntax btw.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值