I am planning to send request to server with the following code. I have spent more than 1 day to resolve this but without any progress. And please forgive me I have to hide the real URL address becuase of Company Security Policy.
import requests
get_ci = requests.session()
get_ci_url = 'https://this_is_a_fake_URL_to_paste_in_stackoverflow.JSON'
get_ci_param_dict = {"Username": "fake","Password": "fakefakefake","CIType": "system","CIID": "sampleid","CIName": "","AttrFilter": "","SubObjFilter": ""}
get_ci_param_str = str(get_ci_param_dict)
print(get_ci_param_dict)
print(get_ci_param_str)
get_ci_result = get_ci.request('POST', url=get_ci_url, params=get_ci_param_str, verify=False)
print(get_ci_result.status_code)
print(get_ci_result.text)
And what I get in the Run result is,
C:\Python34\python.exe C:/Users/this/is/the/fake/path/Test_02.py
{'CIID': 'sampleid', 'CIType': 'system', 'AttrFilter': '', 'Password': 'fake', 'CIName': '', 'Username': 'fake', 'SubObjFilter': ''}
{'CIID': 'sampleid', 'CIType': 'system', 'AttrFilter': '', 'Password': 'fake', 'CIName': '', 'Username': 'fake', 'SubObjFilter': ''}
C:\Python34\lib\requests\packages\urllib3\connectionpool.py:843: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
InsecureRequestWarning)
500
<ns1:XMLFault xmlns:ns1="http://cxf.apache.org/bindings/xformat"><ns1:faultstring xmlns:ns1="http://cxf.apache.org/bindings/xformat">*org.codehaus.jettison.json.JSONException: A JSONObject text must begin with '{' at character 0 of* </ns1:faultstring></ns1:XMLFault>
Process finished with exit code 0
More tips,
I have contact the Server Code Developer - They only need is a string in a JSON format sent in a "Parameter" way. Which means it is correct to use
params
inrequest()
.I have tried with
dumps.json(get_ci_param_dict)
=> The same result.It has returned the 200 code when I only request the server's root, which proves me the url is ok.
Additional Logs when update the params
to data
.
C:\Python34\lib\requests\packages\urllib3\connectionpool.py:843: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
InsecureRequestWarning)
500
<html><head><title>Apache Tomcat/7.0.61 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 500 - 1</h1><HR size="1" noshade="noshade"><p><b>type</b> Exception report</p><p><b>message</b> <u>1</u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u></p><p><b>exception</b> <pre>java.lang.ArrayIndexOutOfBoundsException: 1
com.fake.security.XSSHttpReuquestWrapper.GeneralParameters(XSSHttpReuquestWrapper.java:158)
com.fake.security.XSSHttpReuquestWrapper.checkParameter(XSSHttpReuquestWrapper.java:101)
com.fake.security.XSSHttpReuquestWrapper.validateParameter(XSSHttpReuquestWrapper.java:142)
com.fake.security.XSSSecurityFilter.doFilter(XSSSecurityFilter.java:35)
com.fake.webservice.interceptor.GetContextFilter.doFilter(GetContextFilter.java:24)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
</pre></p><p><b>note</b> <u>The full stack trace of the root cause is available in the Apache Tomcat/7.0.61 logs.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.61</h3></body></html>
Process finished with exit code 0
FINAL SOLUTION FOR THIS QUESTION
@e4c5 your suggestion is helpful to figure out the final solution. The param
should be sent to server via data
, as the data
should be sent out as dict or byte as defined in the offical document, so need use param
as the dict key to send. Please see the code as below,
import requests
import json
get_ci_url = 'https://sample.fake.com:0000/sample/fake/fakeagain.JSON'
get_ci_param_dict = {"Username": "fake","Password": "fakefake".......}
get_ci_param_json = json.dumps(get_ci_param_dict)
params = {'param': get_ci_param_json}
get_ci_result = requests.request('POST', url=get_ci_url, data=params, verify=False)
print(get_ci_result.status_code)
print(get_ci_result.text)
ROOT CAUSE:param
should be sent via data
parameter. Official Document has clearly state that => :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:Request
.
Thanks My colleauge - Mr.J and @e4c5's great help.