简体   繁体   中英

Unable to find out how to convert the below curl request as Python request

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
}'

I tried using uncurl but facing an issue with the data-raw parameter as unable to convert this entire request as single string. Using uncurl shows invalid syntax

You can do something like this:

import requests
import json
headers = 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM