当前位置: 动力学知识库 > 问答 > 编程问答 >

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 -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 :

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.

Thank you,

Subash

网友答案:

At first, the 'url' parameter in requests.post should be an complete url including the 'http://' or 'https://'.
Secondly, if you need to post some data in form of application/json, using Python protogenic dict will be fine. 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

分享给朋友:
您可能感兴趣的文章:
随机阅读: