One question about post request

Hi gays:
When i develop a integrations and running in homeassistant.The post request no answer.
the code is simple.

    url = BASE_API_URL  + "?openid=" + self._openid +"&timestamp=" + self._time_stamp + "&noncestr=" + self._noncestr + "&sign=" + self._sign + "&unionid=" + self._unionid + "&userInfo=null" 
    _LOGGER.debug(f"{url}")
    resp = requests.post(url, headers=base_url_headers, timeout=20)
    _LOGGER.debug(f"{resp.url}")
    _LOGGER.debug(f"{resp.status_code}")

The log is:
2022-10-25 12:11:35.104 DEBUG (MainThread) [custom_components.js_sgcc_energy.jsdw] https://weixin.js.sgcc.com.cn/wxapp_dlsh/wx/oauth_executeNewMini.do?openid=olmSdjqd2XxpgBdfojReFrccPnY0&timestamp=1665300701&noncestr=38b3eff8baf56627478ec76a704e9b52&sign=3afd7eb882840837b0422a736990374dfc39fc80&unionid=oEa171AhIMfwxgYeR2XIJ59FqlFg&userInfo=null

The resp.url and resp.status_code is nothing.

But when i copy the same code to visual studio code then do test.
The running result is:
https://weixin.js.sgcc.com.cn/wxapp_dlsh/wx/oauth_executeNewMini.do?openid=olmSdjqd2XxpgBdfojReFrccPnY0&timestamp=1665300701&noncestr=38b3eff8baf56627478ec76a704e9b52&sign=3afd7eb882840837b0422a736990374dfc39fc80&unionid=oEa171AhIMfwxgYeR2XIJ59FqlFg&userInfo=null
200
https://weixin.js.sgcc.com.cn/wxapp_dlsh/wx/oauth_executeNewMini.do?openid=olmSdjqd2XxpgBdfojReFrccPnY0&timestamp=1665300701&noncestr=38b3eff8baf56627478ec76a704e9b52&sign=3afd7eb882840837b0422a736990374dfc39fc80&unionid=oEa171AhIMfwxgYeR2XIJ59FqlFg&userInfo=null

Why the same code running result different in homeassistant and windows visual studio code.
The windows visual studio code running result is normal,but HA is abnormal.
can you tell me how to do?

Why the same code running result different in homeassistant and windows visual studio code.
The windows visual studio code running result is normal,but HA is abnormal.
can you tell me how to do?

Well, there must be something that is different. Are you sure that the request headers are exactly the same? Can you run the requests through a proxy server to confirm that the request actually goes out? Or use something like Wireshark to look at the traffic? Do you have any insights into the server side or is that just a thirdparty service?

Thank you sir. I am sure the request headers same.But only the code is same i don’t through wireshark to confirm. Windows I can through to comfirm but hassos run on Raspbarry Pi I don’t know how to comfirm.

Yeah, I understand that you have manually defined the same request headers, but due to differen libraries and different operating systems, the default request headers may be different, hence the recommendation to double-check that they are actually the same.

So, can you run your Home Assistant development environment on Windows, too? That gives you more control over the whole process.

Thank you sir. I follow your suggestions I install Home Assistant development environment on Windows then debug the integration,but the result also different with hassos.

In order to confirm the problem I add some debug information. At the same time, I changed post to asynchronous request. The code is following:

async def async_get_baseinf(self):
    _LOGGER.debug("async_get_baseinf runing")
    ret = True
    base_url_headers = self.commonHeaders()
    base_url_params = {
        "openid": self._openid,
        "timestamp": self._time_stamp,
        "noncestr": self._noncestr,
        "sign": self._sign,
        "unionid": self._unionid,
        "userInfo": 'null'         
    }
    _LOGGER.debug(f"{base_url_params}")
    resp = await self._session.post(
        BASE_API_URL, params=base_url_params, headers=base_url_headers, timeout=200
    )        
    _LOGGER.debug(resp.url)
    _LOGGER.debug(resp.status)
    return ret 

Test result:

devcontainer:
2022-11-21 16:39:06.979 DEBUG (MainThread) [custom_components.js_sgcc_energy.jsdw] async_get_baseinf runing

2022-11-21 16:39:06.980 DEBUG (MainThread) [custom_components.js_sgcc_energy.jsdw] {‘openid’: ‘olmSdjqd2XxpgBdfojReFrccPnY0’, ‘timestamp’: ‘1665300701’, ‘noncestr’: ‘38b3eff8baf56627478ec76a704e9b52’, ‘sign’: ‘3afd7eb882840837b0422a736990374dfc39fc80’, ‘unionid’: ‘oEa171AhIMfwxgYeR2XIJ59FqlFg’, ‘userInfo’: ‘null’}

2022-11-21 16:39:07.018 DEBUG (SyncWorker_0) [asyncio] Get address info weixin.js.sgcc.com.cn:443, type=<SocketKind.SOCK_STREAM: 1>, flags=<AddressInfo.AI_ADDRCONFIG: 32>

2022-11-21 16:39:07.079 DEBUG (SyncWorker_0) [asyncio] Getting address info weixin.js.sgcc.com.cn:443, type=<SocketKind.SOCK_STREAM: 1>, flags=<AddressInfo.AI_ADDRCONFIG: 32> took 60.463ms: [(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, ‘’, (‘218.94.127.86’, 443))]

2022-11-21 16:39:07.100 DEBUG (MainThread) [asyncio] <asyncio.sslproto.SSLProtocol object at 0x7f8dcd66d7f0> starts SSL handshake

2022-11-21 16:39:07.126 DEBUG (MainThread) [asyncio] <asyncio.sslproto.SSLProtocol object at 0x7f8dcd66d7f0>: SSL handshake took 25.4 ms

2022-11-21 16:39:07.128 DEBUG (MainThread) [asyncio] <asyncio.TransportSocket fd=17, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=(‘172.17.0.2’, 57862), raddr=(‘218.94.127.86’, 443)> connected to 218.94.127.86:443: (<asyncio.sslproto._SSLProtocolTransport object at 0x7f8db1968c40>, <aiohttp.client_proto.ResponseHandler object at 0x7f8db2cc0100>)

2022-11-21 16:39:07.150 DEBUG (MainThread) [custom_components.js_sgcc_energy.jsdw] https://weixin.js.sgcc.com.cn/wx … g&userInfo=null

2022-11-21 16:39:07.151 DEBUG (MainThread) [custom_components.js_sgcc_energy.jsdw] 200

2022-11-21 16:39:07.159 DEBUG (MainThread) [custom_components.js_sgcc_energy] Finished fetching js_sgcc_energy data in 0.182 seconds (success: True)

2022-11-21 16:39:07.160 DEBUG (MainThread) [custom_components.js_sgcc_energy] Successful to update data, now loading entities

HASSOS:
2022-11-22 00:35:22.303 DEBUG (MainThread) [custom_components.js_sgcc_energy.jsdw] async_get_baseinf runing

2022-11-22 00:35:22.304 DEBUG (MainThread) [custom_components.js_sgcc_energy.jsdw] {‘openid’: ‘olmSdjqd2XxpgBdfojReFrccPnY0’, ‘timestamp’: ‘1665300701’, ‘noncestr’: ‘38b3eff8baf56627478ec76a704e9b52’, ‘sign’: ‘3afd7eb882840837b0422a736990374dfc39fc80’, ‘unionid’: ‘oEa171AhIMfwxgYeR2XIJ59FqlFg’, ‘userInfo’: ‘null’}

2022-11-22 00:35:28.232 ERROR (MainThread) [custom_components.js_sgcc_energy] Error fetching js_sgcc_energy data: Failed to data update with unknown reason

2022-11-22 00:35:28.235 DEBUG (MainThread) [custom_components.js_sgcc_energy] Finished fetching js_sgcc_energy data in 5.932 seconds (success: False)

Testing environment:
1、windows+visual_studio_code+devcontainer
2、Hassos+Raspbarry Pi

I don’t know why,This problem has troubled me for a long time,please tell me how to do next?

why the code
resp = await self._session.post(
BASE_API_URL, params=base_url_params, headers=base_url_headers, timeout=200
)
next two debug statements no reply?
_LOGGER.debug(resp.url)
_LOGGER.debug(resp.status)

Thank you very mach!

Is it possible that there is an exception thrown somewhere? Maybe you can surround your code with a simple try statement and see what happens:

try:
  ... your code ...
except Exception as exc:
  _LOGGER.error(exc)