【问题标题】:Working fine when hitting using Curl but Bad Request 400 when using python requests使用Curl时工作正常,但使用python请求时出现错误请求400
【发布时间】:2020-04-12 10:59:01
【问题描述】:
import requests
import json
import numpy as np

payload = {'query':json.dumps({
         "per_page":12,
         "sort":None,
         "scroll_id":None,
         "session_id":None,
         "q":"diapers",
         "shingle_active":False,
         "location":"110005",
         "types":["allopathy","brand","sku","udp"],
         "country":"",
         "is_query_suggestion_applicable":False,
         "debug":False,
         "filters":None,
         "source_fields":["count"],
         "query_filters":None,
         "is_all":True
       })}
headers = {'Content-Type': 'application/json'}
url = 'https://kjhghfjdslsls' 
r = requests.post(url, params=payload, headers=headers)
print(r.text)

这给出了错误:

{"errors":[{"message":"参数 per_page is required"}],"is_success":false,"status_code":400}

虽然使用以下 curl 请求时它工作正常并返回所需的输出-->

curl --location --request POST 'http://khjfhdksl' \
    --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,
      "source_fields":["count"],
      "query_filters":null,
      "is_all":true
    }'

【问题讨论】:

标签: python curl parameters python-requests bad-request


【解决方案1】:

您在 python 版本的有效负载中有一个顶级密钥 query。您将有效负载作为params(查询参数)传递

尝试删除它:

import requests
import json
import numpy as np

payload = json.dumps({
         "per_page":12,
         "sort":None,
         "scroll_id":None,
         "session_id":None,
         "q":"diapers",
         "shingle_active":False,
         "location":"110005",
         "types":["allopathy","brand","sku","udp"],
         "country":"",
         "is_query_suggestion_applicable":False,
         "debug":False,
         "filters":None,
         "facets":[{"field":"sku.brand.raw","name":"brand","type":"facet","range":None},{"field":"product_form","name":"product_form","type":"facet","range":None},{"field":"rx_required","name":"rx_required","type":"facet","range":None},{"field":"uses","name":"uses","type":"facet","range":None},{"field":"age","name":"age","type":"facet","range":None},{"field":"recommended","name":"recommended","type":"facet","range":None}],
         "source_fields":["count"],
         "query_filters":None,
         "is_all":True
       })
headers = {'Content-Type': 'application/json'}
url = 'https://search-stag123api.example.com/search-new/search/v1/search/lambda' 
r = requests.post(url, data=payload, headers=headers)
print(r.text)

【讨论】:

  • 酷。请随意接受它对您有帮助的答案。
  • 真的!!我从收到的第一条评论中得到了解决方案。已在您的答案中进行了相同的编辑。一旦被接受,将接受它?
  • 使用json 的问题是它会自动将您的Content-Type 更改为application/json - 这可能会导致意想不到的副作用
猜你喜欢
  • 1970-01-01
  • 2021-10-22
  • 1970-01-01
  • 2013-09-27
  • 2015-08-18
  • 2015-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多