如何使用curl将POST请求从python重写为C ++

问题描述 投票:0回答:1

我对python有很多设置的POST请求,但我不理解它们在curl中的外观。

data_str = '{' + '"username": "{}", "domain_id": {}, "password": {}'.format(login, domain_id, password) + '}'
      try:
            data = requests.post("https://example.com/v/session",
                                 proxies=proxy,
                                 verify=False,
                                 data=data_str,
                                 headers={"Content-Type": "application/json;charset=UTF-8",
                                          "Accept": "application/json"})
            if is_json(data.text):
                print(data)

我发现该网址设置了参数CURLOPT_URL,标题-CURLOPT_HTTPHEADER。但是如何设置代理,验证和数据呢?如何在python中获取json?

c++ json python-requests http-post libcurl
1个回答
0
投票

您正在寻找的基本curl命令在下面

curl -D {HEADER_FILE_PATH} -m {TIMEOUT} -H "Authorization: Bearer {SESSION}" -H \"Content-Type: application/json; charset=utf-8\" -d @{REQUEST_FILE_PATH} -X POST {INVOKEURL} -o {RESPONSE_FILE_PATH}

  1. 这里您需要以字符串格式构建Json Request并将其存储在“ REQUEST_FILE”
  2. 最后,您需要从“ RESPONSE_FILE”中解析响应Json
  3. HTTP头详细信息在头文件中可用

示例::

curl -D /tmp/header.txt -m 30 -H "Authorization: Bearer ABCDEFGHIJ12131372637" -H "Content-Type: application/json; charset=utf-8" -d @tmp/upload.txt -X POST your-Remote-URL -o tmp/response.txt

如果有任何错误,请对此进行改进。

© www.soinside.com 2019 - 2024. All rights reserved.