通过POST Python发送长JSON

时间:2016-09-28 09:50:14

标签: python json

我知道这似乎是根本,但我们怎么做呢?例如,

import urllib2
import ssl
import requests
import json

SetProxy={'https':'https://sample.sample.com'}
proxy = urllib2.ProxyHandler(SetProxy)
url="https://something.com/getsomething"
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
payload = {
  "uaaURL": "https://com-example.something.com",
  "sampleID": "admin",
  "sampleSecret": "password",
  "sampleID2": "example-sample-el",
  "sampleSecret2": "ssenjsoemal/+11=",
  "username": "test",
  "someAttributes": {
    "Groups": [
      "example_com-abc"
    ],
    "attribute": [
      "value1"
    ]
  }
}

req = urllib2.Request(url)
req.add_header('Content-Type','application/json')
response = urllib2.urlopen(req, json=json.dumps(payload))

以上运行并给我一个400,这意味着我的JSON是坏的。我无法通过帖子请求找到发送大量JSON数据的帖子。有什么建议?谢谢!

1 个答案:

答案 0 :(得分:-1)

尝试使用以下内容:

import json

import urllib2

import ssl

import requests

SetProxy={'https':'https://sample.sample.com'}

proxy = urllib2.ProxyHandler(SetProxy)

url='https://something.com/getsomething'

opener = urllib2.build_opener(proxy)

urllib2.install_opener(opener)

data = {
  'uaaURL' : 'https://com-example.something.com',
  'sampleID': 'admin',
  'sampleSecret': 'password',
  'sampleID2': 'example-sample-el',
  'sampleSecret2': 'ssenjsoemal/+11=',
  'username': 'test',
  'someAttributes': {
    'Groups': ['example_com-abc' ],
    'attribute': [ 'value1' ]
  }

req = urllib2.Request('https://something.com/getsomething')

req.add_header('Content-Type', 'application/json')

response = urllib2.urlopen(req, json.dumps(data))