简体   繁体   English

我正在使用python调用Rest API Curl命令,但是发布请求不起作用

[英]I am calling a Rest API Curl command using python, But the post request is not working

So here is my curl command, In command line, curl script is working fine 这是我的curl命令,在命令行中,curl脚本运行正常

curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" -u user:password URL -d '{"key1":"value1","key2":"value2"}'

Now I have converted my code to python and the code look like this : 现在,我将代码转换为python,代码如下所示:

import requests
import json

headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
}

data = '{"key1":"value1","key2":"value2"}'

requests.post('URL', headers=headers, data=data, auth=('user', 'password'))

When I ran this code, I am not getting any output. 运行此代码时,没有任何输出。 Please let me know what I am doing wrong in here. 请让我知道我在这里做错了什么。

At first, the 'url' parameter in requests.post should be an complete url including the 'http://' or 'https://'. 首先, requests.post的“ url”参数应为完整的URL,包括“ http://”或“ https://”。
Secondly, if you need to post some data in form of application/json , using Python protogenic dict will be fine. 其次,如果您需要以application/json形式发布一些数据,那么使用Python的protogenic dict就可以了。 like this 像这样
data = {"key1":"value1","key2":"value2"}

you might be looking at something like this? 您可能正在看这样的东西?

r = requests.post("http://{}/Command?".format(web_addr), params=dict)
print r.text

In Python 3.6.1, you should get the response 200 message if the following code gets executed, with a existing 'URL': 在Python 3.6.1中,如果执行以下代码并使用现有的“ URL”,则应该收到响应200消息:

from requests import post
data = {"key1":"value1","key2":"value2"}
print(post('URL', json=data))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么通过python请求调用rest api时出现500 Internal Server Error? - Why am I getting 500 Internal Server Error when calling post rest api via python request? 我正在使用 python 的 request.post 方法来调用 API 的 post 方法 开发于 C# - I am using request.post method of python for calling post method of API Developed in C# 如何将 curl 命令作为 python 请求发布? - How do I post curl command as a python request? 我需要帮助将此REST API Curl命令转换为Python请求 - I need help converting this REST API Curl command to Python requests 使用 Python requests.post() 方法在调用 REST API 时获取 HTTP 502,同时在 REST 客户端上工作正常 - Getting HTTP 502 on calling a time taking REST API using Python requests.post() method while same is working fine with REST client 将curl post转换为REST API到Python - convert curl post to rest api to python 如何在 python 中为此 curl 命令发送发布请求 - how to send post request in python for this curl command 将curl rest api命令“转换”为python - “convert” curl rest api command to python 使用 python 发出 POST 请求失败,而使用 curl 发出相同请求成功。 我错过了什么? - Making a POST request with python fails while making the same request with curl succeeds. What am I missing? cURL POST 请求不能像 python 请求一样工作 - cURL POST request not working as python requests equivalent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM