Работает нормально при нажатии Curl, но Bad Request 400 при использовании python запросов - PullRequest
0 голосов
/ 12 апреля 2020
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,
         "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, params=payload, headers=headers)
print(r.text)

Это дает ошибку:

{"errors": [{"message": "Требуется параметр per_page"}], "is_success": false, "status_code": 400 }

Хотя при использовании приведенного ниже запроса curl он работает нормально и возвращает требуемый вывод ->

curl --location --request POST 'https://search-stagapi.1mg.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
    }'

Ответы [ 2 ]

0 голосов
/ 13 апреля 2020
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, json=payload, headers=headers)
print(r.text)

Эта версия работает нормально, когда я изменил параметры на json и просто передал им полезную нагрузку после преобразования в json без какого-либо ключа запроса

0 голосов
/ 12 апреля 2020

У вас есть ключ верхнего уровня query в полезной нагрузке для версии python. И вы передаете полезную нагрузку как 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)
...