I have cURL code that I need to convert to python code using requests library. I want to use this inside django backend software.
I tried several ways but I am getting some erros. Can someone help me with this?
Here is the cURL code:
curl -XPOST -H 'cache-control: no-cache' -H 'content-type: application/json' -H
'X-Client-Id: asdf1234' -H 'X-Client-Secret: qwer9876' -d '{
"planId":"BASIC", "planName":"Basic subscription plan", "amount":12,
"intervalType":"week", "intervals":2,"description":"This is the standard plan
for our services"}' 'https://test.cashfree.com/api/v2/subscription-plans'
In future, you can use a handy converter like this:
import requests
headers = {
'cache-control': 'no-cache',
'content-type': 'application/json',
'X-Client-Id': 'asdf1234',
'X-Client-Secret': 'qwer9876',
}
data = '{"planId":"BASIC", "planName":"Basic subscription plan", "amount":12,"intervalType":"week", "intervals":2,"description":"This is the standard planfor our services"}'
response = requests.post('https://test.cashfree.com/api/v2/subscription-plans', headers=headers, data=data)
Related
working curl command:
curl -i -XPATCH "https://api.threatstream.com/api/v1/intelligence/"
-H "Content-Type: application/json"
-H "Authorization: apikey email#email.com:password"
--data #C:\Users\ghossh6\indicators.json
requests:
import requests
headers = {
'Content-Type': 'application/json',
'Authorization': 'apikey email#email.com:password',
}
data = open("C:/Users/ghossh6/indicators.json")
response = requests.patch('https://api.threatstream.com/api/v1/intelligence/', headers=headers, data=data)
Response
Currently, I only get 502 or 405 error codes. I have tried using json.loads() to load the file instead, without success.
I have this curl command:
curl -X GET --header 'Accept: application/vnd.pagerduty+json;version=2' --header 'Authorization: Token token=y_NbAkKc66ryYTWUXYEu' 'https://api.pagerduty.com/services?time_zone=UTC&sort_by=name'
I need to convert it to python using requests library
import requests
def support(self):
services_list = requests.get('I need to convert the link to pass it here as a parameter')
import requests
def support():
headers = {
'Accept': 'application/vnd.pagerduty+json;version=2',
'Authorization': 'Token token=y_NbAkKc66ryYTWUXYEu'}
payloads = (
('time_zone', 'UTC'),
('sort_by', 'name'),)
services_list = requests.get('https://api.pagerduty.com/services', headers=headers, params=payloads)
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.
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
In Python 3.6.1, you should get the response 200 message if the following code gets executed, with a existing 'URL':
from requests import post
data = {"key1":"value1","key2":"value2"}
print(post('URL', json=data))
I'm trying to use pycurl for the first time and I'm not sure how to use parameters with it.
curl "https://mytest.com/mdb.json"
-H "Cookie: JSESSIONID=6CCB148AEE7318BD08EFC869E0FD33AB; user=testuser; wmUserPrincipal="%"7B"%"22username"%"22"%"3A"%"22testuser"%"22"%"2C"%"22roles"%"22"%"3A"%"5B"%"5D"%"7D; mf_user=322383eec941db6c72f3f2c7d58b7a80"
-H "Content-Type: application/json-rpc"
-H "Accept: */*"
-H "X-Requested-With: XMLHttpRequest"
-H "Connection: keep-alive" --data-binary "{""params"":[""1.2.1"",""instance7"",""1.2"",4,{}],""method"":""getMatrix"",""id"":250}" --compressed
As seen in the curl I have:
--data-binary "{""params"":[""1.2.1"",""instance7"",""1.2"",4,{}],""method"":""getMatrix"",""id"":250}" --compressed
and I cannot figure out what to do with them in pycurl. Maybe it is not even possible or maybe there is a simpler solution than using pycurl.
Thank you!
Got it running with requests which is a simpler and exactly what I needed.
cookie = {
'JSESSIONID': '6CCB148AEE7318BD08EFC869E0FD33AB',
'user': 'testuser',
'wmUserPrincipal': '%7B%22username%22%3A%22testuser%22%2C%22roles%22%3A%5B%5D%7D',
'mf_user': '322383eec941db6c72f3f2c7d58b7a80',
}
head = {
'Content-Type': 'application/json-rpc',
'Accept': '*/*',
'X-Requested-With': 'XMLHttpRequest',
'Connection': 'keep-alive',
}
data = '{"params":["1.2.1","instance7","1.2",4,{}],"method":"getMatrix","id":250}'
requests.post('https://mytest.com/mdb.json', headers=head, cookies=cookie, data=data)
I have a Python script using the Requests library that is of this form:
uhash = '1234567abcdefg'
cookies = {
'uhash':uhash
}
payload = {
'action':'trade.bump',
'hash':uhash,
'tradeid':'12345678'
}
r = requests.post(
'http://www.target_url.com/api/core',
cookies=cookies,
params=payload
)
Above is my Python attempt at creating the following cURL request (written with bash):
HASH="1234567abcdefg"
TRADEID="12345678"
curl 'http://www.target_url.com/api/core' -H "Cookie: uhash=$HASH" --data "action=trade.bump&hash=$HASH&tradeid=$TRADEID"
In summary, both scripts contain:
The cookie - uhash
Three data parameters called action, hash, and tradeid
My issue currently is, the bash script works - the server response for when I use the bash script is this:
{"meta":{"code":200},"data":{"bumped":true,"count":15}}
However, if I use the Python script, with the SAME cookie and parameter values as the bash script, I get:
{"meta":{"code":301},"data":{"message":"You can't bump a trade that doesn't exist ;_;"}}
The above error tells me the trade doesn't exist, despite that tradeid existing and the exact same one as my bash script's parameters.
I tried to debug using Firefox' convenient copy-as-curl tool to copy that curl command, which was how I made the bash script. However, once I tried to translate it to the Python script, it will tell me the aforementioned error. Maybe I am using the Requests library incorrectly, and I am missing something.
Attached is the full cURL request taken from Firefox (don't worry, the parameters were sanitized, meaning, they're not the real values):
curl 'http://www.tf2outpost.com/api/core' -H 'Host: www.tf2outpost.com' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:35.0) Gecko/20100101 Firefox/35.0' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'X-Requested-With: XMLHttpRequest' -H 'Referer: http://www.tf2outpost.com/trades' -H 'Cookie: __qca=P0-6517545-1420724809746; __utma=5135382.11011755.14224810.14331180.14180489.7; __utmz=51353782.1420724810.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); uhash=abcdefg12345678; mb_uid2=3211475230616776; CTag61=14338638870; __utmb=513532.9.10.14180489; __utmc=513782; __utmt=1; __utmt_b=1; __utmt_c=1; OX_plg=sl|qt|pm; HIRO_COOKIE=data=&newSession=true&id=2237524293×tamp=1433506185; HIRO_CLIENT_ID=67751187' -H 'Connection: keep-alive' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' --data 'action=trade.bump&hash=abcdefg12345678&tradeid=12345678'
Not quite sure why that is happening.
Try using data or json key instead of params, use json.dumps(payload) if data is your preferred method.