简体   繁体   English

有没有办法使用 python requests 模块形成批处理请求? 我想在单个 POST 请求中发送多个同质 http API 请求

[英]Is there a way to form batched requests using python requests module ? I want to send multiple homogenous http API requests in a single POST request

Is there a way to form batched requests using python requests module?有没有办法使用 python requests 模块形成批处理请求? I want to send multiple homogenous http API requests in a single POST request.我想在单个 POST 请求中发送多个同质 http API 请求。 I am trying to use the GCP documentation https://cloud.google.com/dns/docs/reference/batch?hl=en_US&_ga=2.138235123.-2126794010.1660759555 to create multiple DNS records in a single POST request.我正在尝试使用 GCP 文档https://cloud.google.com/dns/docs/reference/batch?hl=en_US&_ga=2.138235123.-2126794010.1660759555在单个 POSTZ 请求中创建多个 ZED5F2BDECBD4BD349D09412D1FF6 记录。 Can anyone help with a simple example of how this could be achieved using python requests module?谁能提供一个简单的例子来说明如何使用 python 请求模块来实现这一点?

This is how the sample POST request will look like:这是示例 POST 请求的样子:

POST /batch/farm/v1 HTTP/1.1
Authorization: Bearer your_auth_token
Host: www.googleapis.com
Content-Type: multipart/mixed; boundary=batch_foobarbaz
Content-Length: total_content_length

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item1:12930812@barnyard.example.com>

GET /farm/v1/animals/pony

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item2:12930812@barnyard.example.com>

PUT /farm/v1/animals/sheep
Content-Type: application/json
Content-Length: part_content_length
If-Match: "etag/sheep"

{
  "animalName": "sheep",
  "animalAge": "5"
  "peltColor": "green",
}

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item3:12930812@barnyard.example.com>

GET /farm/v1/animals
If-None-Match: "etag/animals"

--batch_foobarbaz--

Basically;基本上; the main intention here is to not overload the remote API with multiple http requests causing the rate limit throttling but instead use batched http requests so that the remote API gets only a single batched request embedded with multiple requests in the form of parts. the main intention here is to not overload the remote API with multiple http requests causing the rate limit throttling but instead use batched http requests so that the remote API gets only a single batched request embedded with multiple requests in the form of parts.

No. HTTP doesn't work that way.号 HTTP 不能那样工作。 You can send multiple requests simultaneously using threads, but you can't send multiple POSTs through a single request.您可以使用线程同时发送多个请求,但不能通过单个请求发送多个 POST。

According to the doc, the individual batch HTTP requests are supposed to go in the body of the request.根据文档,单个批次 HTTP 请求应该是请求正文中的 go 。 You can try building the requests up manually.您可以尝试手动构建请求。 Like so:像这样:

import requests

body = """
--batch_foobarbaz
Content-Type: application/http
Content-ID: <item1:12930812@barnyard.example.com>

GET /farm/v1/animals/pony

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item2:12930812@barnyard.example.com>

PUT /farm/v1/animals/sheep
Content-Type: application/json
Content-Length: part_content_length
If-Match: "etag/sheep"

{
  "animalName": "sheep",
  "animalAge": "5"
  "peltColor": "green",
}

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item3:12930812@barnyard.example.com>

GET /farm/v1/animals
If-None-Match: "etag/animals"

--batch_foobarbaz--
"""

response = requests.post(
    "https://www.googleapis.com/batch/API/VERSION/batch/form/v1",
    data=body,
    headers={
        'Authorization': 'Bearer your_auth_token',
        'Host': 'www.googleapis.com',
        'Content-Type': 'multipart/mixed; boundary=batch_foobarbaz'
    }
)

Of course, you'd have to build the individual requests manually:当然,您必须手动构建各个请求:

Content-Type: application/http
Content-ID: <item1:12930812@barnyard.example.com>

GET /farm/v1/animals/pony

Unless you can find a library that can construct HTTP requests according to the HTTP/1.1 RFC .除非你能找到可以按照HTTP/1.1 RFC构造 HTTP 请求的库。 I can't think of one off the top of my head.我想不出来一个。

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

相关问题 使用 python 中的请求将发布请求发送到具有多个表单标签的网站 - send a post request to a website with multiple form tags using requests in python 如何使用Python Requests模块模拟HTTP发布请求? - How to simulate HTTP post request using Python Requests module? 使用python请求发送带有unicode数据的http post请求 - Send http post request with unicoded data using python requests 如何使用python请求在同一连接中发送多个post请求 - How to send multiple post requests in same connection using python requests 如何使用 Python 发送多个 HTTP 请求 - How to send multiple HTTP requests using Python 使用 Python 和请求模块发布 - Using Python and requests module to post 如何通过 http 使用 requests 模块和 Z9784E21C7B2AFDD678Z87 发布请求发送 numpy 数组或 pytorch 张量 - how to send an numpy array or a pytorch Tensor through http post request using requests module and Flask 使用 requests 模块模拟 HTTP 发布请求不起作用 - Simulating HTTP post request using requests module is not working 无法通过python请求模块发送`multipart / form-data`请求 - Unable to send `multipart/form-data` request with python requests module Python请求发送带有表单数据输入的POST请求 - Python requests to send POST request with form-data input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM