无法找出如何将以下 curl 请求转换为 Python 请求

curl --location --request POST 'https://search-stagapi.example.com/search-new/search/v1/search/lambda' \

--header 'Content-Type: application/json' \

--header 'Content-Type: application/json' \

--data-raw '{

  "sort":null,

  "per_page":12,

  "scroll_id":null,

  "session_id":null,

  "q":"diapers",

  "shingle_active":false,

  "location":"110005",

  "types":["allopathy","brand","sku","udp"],

  "country":"",

  "is_query_suggestion_applicable":false,

  "debug":false,

  "filters":null,

  "facets":[{"field":"sku.brand.raw","name":"brand","type":"facet","range":null},{"field":"product_form","name":"product_form","type":"facet","range":null},{"field":"rx_required","name":"rx_required","type":"facet","range":null},{"field":"uses","name":"uses","type":"facet","range":null},{"field":"age","name":"age","type":"facet","range":null},{"field":"recommended","name":"recommended","type":"facet","range":null}],

  "source_fields":["count"],

  "query_filters":null,

  "is_all":true

}'

我尝试使用 uncurl,但遇到了 data-raw 参数的问题,因为无法将整个请求转换为单个字符串。使用 uncurl 显示无效语法


慕森王
浏览 103回答 1
1回答

慕妹3242003

你可以这样做:import requestsimport jsonheaders = headers = {'content-type': 'application/json'}payload = {json.dumps({  "sort":null,  "per_page":12,  "scroll_id":null,  "session_id":null,  "q":"diapers",  "shingle_active":false,  "location":"110005",  "types":["allopathy","brand","sku","udp"],  "country":"",  "is_query_suggestion_applicable":false,  "debug":false,  "filters":null,  "facets":[{"field":"sku.brand.raw","name":"brand","type":"facet","range":null},{"field":"product_form","name":"product_form","type":"facet","range":null},{"field":"rx_required","name":"rx_required","type":"facet","range":null},{"field":"uses","name":"uses","type":"facet","range":null},{"field":"age","name":"age","type":"facet","range":null},{"field":"recommended","name":"recommended","type":"facet","range":null}],  "source_fields":["count"],  "query_filters":null,  "is_all":true})}url = 'https://search-stagapi.example.com/search-new/search/v1/search/lambda' r = requests.post(url, params=payload, headers=headers)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python