python post form data_在python中创建POST请求,需要将数据作为multipart / form-data发送吗?...

I'm in the process of writing a very simple Python application for a friend that will query a service and get some data in return. I can manage the GET requests easily enough, but I'm having trouble with the POST requests. Just to get my feet wet, I've only slightly modified their example JSON data, but when I send it, I get an error. Here's the code (with identifying information changed):

import urllib.request

import json

def WebServiceClass(Object):

def __init__(self, user, password, host):

self.user = user

self.password = password

self.host = host

self.url = "https://{}/urldata/".format(self.host)

mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()

mgr.add_password(None, "https://{}".format(self.host), self.user, self.password)

self.opener = urllib.request.build_opener(urllib.request.HTTPDigestAuthHandler(mgr))

username = "myusername"

password = "mypassword"

service_host = "thisisthehostinfo"

web_service_object = WebServiceClass(username, password, service_host)

user_query = {"searchFields":

{

"First Name": "firstname",

"Last Name": "lastname"

},

"returnFields":["Entity ID","First Name","Last Name"]

}

user_query = json.dumps(user_query)

user_query = user_query.encode("ascii")

the_url = web_service_object.url + "modules/Users/search"

try:

user_data = web_service_object.opener.open(the_url, user_query)

user_data.read()

except urllib.error.HTTPError as e:

print(e.code)

print(e.read())

I got the class data from their API documentation.

As I said, I can do GET requests fine, but this POST request gives me a 500 error with the following text:

Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

In researching this error, my assumption has become that the above error means I need to submit the data as multipart/form-data. Whether or not that assumption is correct is something I would like to test, but stock Python doesn't appear to have any easy way to create multipart/form-data - there are modules out there, but all of them appear to take a file and convert it to multipart/form-data, whereas I just want to convert this simple JSON data to test.

This leads me to two questions: does it seem as though I'm correct in my assumption that I need multipart/form-data to get this to work correctly? And if so, do I need to put my JSON data into a text file and use one of those modules out there to turn it into multipart, or is there some way to do it without creating a file?

解决方案

Maybe you want to try the requests lib, You can pass a files param, then requests will send a multipart/form-data POST instead of an application/x-www-form-urlencoded POST. You are not limited to using actual files in that dictionary, however:

import requests

response = requests.post('http://httpbin.org/post', files=dict(foo='bar'))

print response.status_code

If you want to know more about the requests lib, and specially in sending multipart forms take a look at this:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值