Преобразование оператора POST curl с полезной нагрузкой JSON в запрос Python - PullRequest
0 голосов
/ 09 октября 2019

Я могу запустить это состояние curl с curl и работает отлично. Я прочитал много постов, но ничего не работает.

curl -X POST "http://some.website.com" -H "accept: application/json" -H "authorization: Basic authcode" -H "Content-Type: application/json" -d "{ \"Fields\": [ \"string\" ], \"Filters\": [ { \"Field\": \"Item\", \"Operator\": \"=\", \"Value\": \"119001\" } ], \"PageSize\": 0, \"PageNumber\": 0}"

код уже пробовал

import requests

session = requests.Session()

url = 'http://some.website.com'

headers = {'accept': 'application/json', 'authorization': 'Basic authcode', 'Content-Type': 'application/json'}

data = {'Fields': 'string', 'Filters': { 'Field': 'Item', 'Operator': '=', 'Value': '119001' }, 'PageSize': 0, 'PageNumber': 0}

response = session.post(url, headers=headers, data=data)

print(response.status_code)
print(response.json())

Ошибка = недопустимо Значение JSON

Я также пытался

import simplejson as json
# ...
# ...
response = session.post(url, headers=headers, data=json.dumps(data))
# ...
# ...

Не удалось = Ошибкаобнаружение полей JSON

Я думаю, что это как-то связано с вложенным оператором dict

1 Ответ

0 голосов
/ 09 октября 2019

Использование https://httpbin.org/post Я вижу, какие данные (заголовки и тело) получены на сервере, и я вижу тот же результат для curl

curl -X POST "http://httpbin.org/post" -H "accept: application/json" -H "authorization: Basic authcode" -H "Content-Type: application/json" -d "{\"Fields\": [\"string\"], \"Filters\": [{\"Field\": \"Item\", \"Operator\": \"=\", \"Value\": \"119001\"}], \"PageSize\": 0, \"PageNumber\": 0}"

# result 

{
  "args": {}, 
  "data": "{\"Fields\": [\"string\"], \"Filters\": [{\"Field\": \"Item\", \"Operator\": \"=\", \"Value\": \"119001\"}], \"PageSize\": 0, \"PageNumber\": 0}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "application/json", 
    "Authorization": "Basic authcode", 
    "Content-Length": "122", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.58.0"
  }, 
  "json": {
    "Fields": [
      "string"
    ], 
    "Filters": [
      {
        "Field": "Item", 
        "Operator": "=", 
        "Value": "119001"
      }
    ], 
    "PageNumber": 0, 
    "PageSize": 0
  }, 
  "origin": "83.23.32.69, 83.23.32.69", 
  "url": "https://httpbin.org/post"
}

и Python

import requests

headers = {
    'Accept': 'application/json',
    'Authorization': 'Basic authcode',
#    'Content-Type': 'application/json',
#    'User-Agent': 'Mozilla/5.0',
}

data = {
  "Fields": [ "string" ],
  "Filters": [ { "Field": "Item", "Operator": "=", "Value": "119001" } ],
  "PageSize": 0,
  "PageNumber": 0
}

response = requests.post('https://httpbin.org/post', headers=headers, json=data)
print(response.text)

# result

{
  "args": {}, 
  "data": "{\"Fields\": [\"string\"], \"Filters\": [{\"Field\": \"Item\", \"Operator\": \"=\", \"Value\": \"119001\"}], \"PageSize\": 0, \"PageNumber\": 0}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "application/json", 
    "Accept-Encoding": "gzip, deflate", 
    "Authorization": "Basic authcode", 
    "Content-Length": "122", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.22.0"
  }, 
  "json": {
    "Fields": [
      "string"
    ], 
    "Filters": [
      {
        "Field": "Item", 
        "Operator": "=", 
        "Value": "119001"
      }
    ], 
    "PageNumber": 0, 
    "PageSize": 0
  }, 
  "origin": "83.23.32.69, 83.23.32.69", 
  "url": "https://httpbin.org/post"
}

Существуют только различия в заголовках: "User-Agent": "curl/7.58.0" и "User-Agent": "python-requests/2.22.0". И Python использует "Accept-Encoding": "gzip, deflate".

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...