Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用POST/REST和请求发送json的脚本不起作用_Python_Json_Rest_Post - Fatal编程技术网

在python中使用POST/REST和请求发送json的脚本不起作用

在python中使用POST/REST和请求发送json的脚本不起作用,python,json,rest,post,Python,Json,Rest,Post,我尝试使用REST在POST方法中发送json,但出现错误: "Could not parse JSON data: Expecting value: line 1 column 1 (char 0)", "status": 500, "type": "ValueError", "request": {"client_addr": "127.0.0.1", "user_agent": "python-requests/2.3.0 CPython/3.4.2

我尝试使用REST在POST方法中发送json,但出现错误:

    "Could not parse JSON data: Expecting value: line 1 column 1 
    (char 0)", "status": 500, "type": "ValueError", 
    "request": {"client_addr": "127.0.0.1", 
    "user_agent": "python-requests/2.3.0 CPython/3.4.2 Linux/3.16.0-41-generic", 
    "method": "POST", "path": "/api/adley/doc"}}
    )
我试图修复,使用json.dumps或json.loads,但似乎没有任何效果

我需要发送一个键和两个值。以下是基础:

   {
metadata: {
   idx_exp: false,
   idx_exp_time: 0,
   file_ext: false,
   password: "",
   description: "base de teste",
   idx_exp_url: "",
   model: {
      item: "Text"
   },
   dt_base: "29/06/2015 14:47:10",
   name: "adley",
   id_base: 5,
   file_ext_time: 0,
   },
   content: [
   {
    field: {
          required: false,
          multivalued: false,
          alias: "Texto",
          datatype: "Text",
          name: "item",
          indices: [
               "Ordenado"
          ],
          description: "placeholder"
         }
     }
  ]
}
要发送帖子脚本,请执行以下操作:

import requests, json
url = "http://127.0.0.1/api/adley/doc"
json_data = {'value':{'item':'test'}}
response = requests.post(url, params=json_data)
print(response.text)
我看不出我的脚本有什么问题

请尝试将数据参数而不是params参数传递给post方法。请求希望在需要json数据的位置对参数进行url编码。比如说:

requests.post(url, data=json.dumps(json_data))

我不明白这个基本代码段是什么,或者它与您的发布脚本有什么关系。脚本只是在基本代码中插入键和值。当工作时,我将创建一个循环来插入指定数量的数据。当我删除json.dumps时,执行此无效参数时出错:{\value\:{\item\:\jorge\},我发现:无法解析JSON数据:期望值:第1行第1列字符0