关于http:为什么我的curl命令工作而不是我的python代码?

Why is my curl command working and not my python code?

我有一个完美的curl命令

1
curl -X POST"https://api.optconnect.com/summit/beta/accounts/login/app_secret" -H "accept: application/json" -H "content-type: application/json" -d"{  "accountId": 000,  "applicationId": 000,  "secret": "000"}"

但是我的python代码给了我一个错误的请求错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import urllib2
import json

url = 'https://api.optconnect.com/summit/beta/accounts/login/app_secret'

data = {'accountId': '000',
    'applicationId': '000',
    'secret': '000'}
data =json.dumps(data)

headers = {'accept': 'application/json', 'content-type': 'application/json' }


requestt = urllib2.Request(url, data ,headers)
response = urllib2.urlopen(requestt)
result = response.read()
print(result)

File"testing.py", line 19, in
response = urllib2.urlopen(requestt)
File"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 437, in open
response = meth(req, response)
File"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 550, in http_response
'http', request, response, code, msg, hdrs)
File"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 475, in error
return self._call_chain(*args)
File"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 558, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request

我错过了什么?


curl JSON中,accountIdapplicationId的值是整数,而不是字符串。 这可能会对应用程序产生影响。 所以使用:

1
2
3
data = {'accountId': 0,
        'applicationId': 0,
        'secret': '000'}