简体   繁体   English

为什么在调用Web服务时我的curl请求有效,而python3请求却无效?

[英]Why does my curl request work and my python3 request not work when calling a webservice?

So I have two (what I think are) identical web service requests...one is from curl which works, and the other is in Python3. 因此,我有两个(我认为是)相同的Web服务请求...一个来自可以工作的curl,另一个来自Python3。

I don't understand the difference between them because the curl one works and returns the expected result, and the Python3 one returns a 500 HTTP status code. 我不了解它们之间的区别,因为curl可以工作并返回预期的结果,而Python3则返回500 HTTP状态代码。

My curl call is as follows: 我的curl调用如下:

$ curl -d "{\"Service\": \"Activity\", \"Action\": \"GetDataHistory\",\"Data\": \"{'StartTime':'2019-08-20T13:00:00+00:00', 'EndTime':'2019-08-20T13:10:00+00:00','GameId':'XXXXXXXX-YYYY-ZZZZ-AAAA-BBBBBBBBBBBB'}\"}" -v -H "Content-Type: application/json" -H "cache-control: no-cache" -H "username: someusrname" -H "password: $(echo $vlpasswd)" -X POST -k https://xxx.xxx.xxx.xxx/api/Service/Call | /c/Users/meacct/AppData/Local/Continuum/anaconda3/python -m json.tool 

And my Python3 code that "should" do the same thing is here: 我的“应该”做同样的事情的Python3代码在这里:

import json
import requests as req
import urllib3

endpoint = 'https://xxx.xxx.xxx.xxx/api/Service/Call'

h = {'Content-Type': 'application/json',
           'cache-control': 'no-cache',
           'username': 'someusrname',
           'password':'das-passwd'
          }

d = {'Service': 'Activity', 
     'Action': 'GetDataHistory',
     'Data': 
     {
             "StartTime":"2019-08-20T13:00:00+00:00", 
             "EndTime":"2019-08-20T13:10:00+00:00",
             "GameId":"XXXXXXXX-YYYY-ZZZZ-AAAA-BBBBBBBBBBBB"
     }
}


# Shut up urllib3's warnings about an invaild SSL Certy.
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

session = req.Session()

try:
    res = session.post(url=endpoint, headers=h, data=d, verify=False)
except req.exceptions.SSLError:
    print("SSL Error")

print(res.status_code)

res.text

and the output is: 输出为:

500 
'{"Message":"An error has occurred."}'

Why would this be when it looks to be the exact same call? 当看起来完全相同时,为什么会出现这种情况?

As the curl request is successful, this indicates that there is something wrong in your Python request, as opposed to the server configuration. 当curl请求成功时,这表明与服务器配置相反,Python请求中存在问题。

Instead of tracking down the issue, I suggest you try using curlconverter , to generate the Python request directly from the curl request. 建议不要尝试使用curlconverter来直接从curl请求生成Python请求,而不是找出问题所在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM