I'm trying to use electrum wallet
I have this code :
import json
import requests
if __name__ == "__main__":
headers = {'content-type': "application/json", 'cache-control': "no-cache"}
payload = json.dumps({"method": 'listaddresses', "params": []})
url = f"http://127.0.0.1:7777"
print(payload)
response = requests.request("POST", url, data=payload, headers=headers,
auth=('user', 'mypass'))
print(response)
print(response.text)
print(response.json)
But i got this error :
<Response [500]>
Invalid Request
I should get an error, but not a 500 error.
It's not lot of info :/ Do you have any idea of what i'm doing wrong ?
after night i found the error.
The payload should be like this :
payload = json.dumps({"jsonrpc":"2.0","id":"curltext", "method": method, "params": params})
with jsonrpc & id
Related
I want to call POST from micropython to Django webserver.
The POST payload shows up as empty when called from the micropython code.
When the same code is ran with C-Python, it does show up with the filled payload.
Here is the micropython code -
import urequests as requests
url = "http://192.168.0.33:8000/species/"
payload={'name': 'test12',
'classification': 'sheep12',
'language': 'sheep_lang12'}
files=[
]
headers = { 'Content-Type': 'application/x-www-form-urlencoded'}
response = requests.request("POST", url, headers=headers, data=payload)
print('success')
print(response.text)
The output of response.txt is -
{'name':'',classification:'',language:''}
I also tried using an alternative of the payload to use JSON. Here is the JSON payload attempt
import urequests as requests
import ujson as json
url = "http://192.168.0.33:8000/species/"
payload={'name': 'test12',
'classification': 'sheep12',
'language': 'sheep_lang12'}
files=[
]
headers = { 'Content-Type': 'application/x-www-form-urlencoded'}
response = requests.request("POST", url, headers=headers, data=json.dumps(payload))
print('success')
print(response.text)
What did I miss here ?
I am working on the requests library in python where I am requesting the API of Bugzilla. The API works fine in postman but the script which I have written from the below link is not working and sending the response back as Bad Request 400. I have added the code below.
Get bugs from bugzilla using python
import requests
import json
URL = 'https://bugzilla.mozilla.org/rest/bug/35'
API_KEY = "api_key"
headers = {
"content-type": "application/json"
}
params = {
"api_key": API_KEY,
}
try:
response = requests.get(URL, headers=headers, params=params)
if response.status_code != 200:
print(f'error : {response.status_code}')
print(response.reason)
else:
print('Success')
print(json.load(response.text))
except response:
print(response)
Probably you are sending wrong api key
try to get your api key via:
r = requests.get('https://bugzilla.mozilla.org/rest/login?login=username&password=password')
r.text
https://wiki.mozilla.org/Bugzilla:REST_API
I've been trying to make a Post Request to my CRM API. The CRM API is very vague:
"You can use POST request when you wish to create records. You POST a JSON encoded string to the servers and it will return a single instance of the record."
POST /{object_name}
Example:
Request URL (POST):
/accounts
Request Body (JSON):
{
"name": "testing API"
}
I've had plenty of success making GET requests regularly, but POST is not working out so easily.
url = "https://apiv4.reallysimplesystems.com/accounts?<KEY>"
payload = {"name":"Ziggy","owner":"XYZ","addresscounty/state":"Awe","source":"Space"}
headers = {
'Content-Type': 'application/json',
'Cookie': 'XSRF-TOKEN=<TOK>; really_simple_systems_session=<KEY>'
}
response = requests.post(url, headers=headers, data=payload)
I get a status code 200 when I run this, but I'm really looking for that 201. The only clue that I've got to follow at this point is that when I run:
response.json()
I get the error:
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I've tried switching the response parameters to json:
response = requests.post(url, headers=headers, json=payload)
I've tried ensuring that my payload is json by using json.dumps():
payload = {"name":"Ziggy","owner":"XYZ","addresscounty/state":"Awe","source":"Space"}
payload = json.dumps(payload)
And I've tried all sorts of other shenanigans that I can't even recall at this point. Does anyone have any idea where I'm going wrong here? The 200 status code makes me feel painfully close.
Replace <AUTH_TOKEN> with your auth token
url = "https://apiv4.reallysimplesystems.com/accounts/"
payload = {"name":"Ziggy"}
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer <AUTH_TOKEN>',
}
response = requests.post(url, headers=headers, data=payload)
Problem solved:
import json
import requests
url = "https://apiv4.reallysimplesystems.com/accounts"
payload = {"name":"RSS Test Joe Bloggs","addresscounty/state":"Maryland","source":"Word of Mouth"}
payload = json.dumps(payload)
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer <AUTH KEY>'
}
response = requests.post(url, headers=headers, data=payload)
Rather than using Postman's code which included the in the URL and used :
'Cookie': 'XSRF-TOKEN=<TOK>; really_simple_systems_session=<KEY>'
I replaced it with a standard Authorization header. Secondly, I found that using json.dumps(payload) and json = payload was resulting in a Bad Request.
Could someone take a look at this code and let me know if there's something I'm doing wrong:
import requests
url = "https://api-end-point"
payload = {"grant_type": "client_credentials", "client_id":"my_ic", "client_secret": "not_now_please"}
headers = '{"accept": "application/json", "content-type": "application/json"}'
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
For some reason it's throwing error.
{"type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html","title":"Bad Request","status":400,"detail":"JSON decoding error: Syntax error, malformed JSON"}
So first use json instead of data, second don't use ' around the headers, or actually you don't need to specify headers:
import requests
url = "https://api-end-point"
payload = {"grant_type": "client_credentials", "client_id":"my_ic", "client_secret": "not_now_please"}
response = requests.request("POST", url, json=payload)
print(response.text)
The page does not contain json/application but html/text. If you remove the headers then it works:
response = requests.request(method="POST", url=url, data=payload)
If you have special needs to specify in the headers add details to your question
I am attempting to access the NJTransit API, I have successfully queried it using the Postman application but no matter what I try I cannot get python to successfully return the desired query.
Using suds:
from suds.client import Client
url = "http://traindata.njtransit.com:8090/NJTTrainData.asmx?wsdl"
client = Client(url, cache = None)
from suds.sax.element import Element
auth = Element('UserCredentials')
auth.append(Element('userName').setText(my_username))
auth.append(Element('password').setText(my_password))
client.set_options(soapheaders = auth)
client = Client(url, cache = None)
result = client.service.getTrainScheduleJSON("NY")
This results in "none".
I've also attempted to use the preformatted request suggested by the Postman app, however I keep getting a 404 error.
import requests
url = "http://traindata.njtransit.com:8090/NJTTrainData.asmx"
querystring = {"wsdl":""}
payload = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n <soap:Header>\n <UserCredentials xmlns=\"http://microsoft.com/webservices/\">\n <userName>---</userName>\n <password>---</password>\n </UserCredentials>\n </soap:Header>\n <soap:Body>\n <getTrainScheduleJSON xmlns=\"http://microsoft.com/webservices/\">\n <station>NY</station>\n </getTrainScheduleJSON>\n </soap:Body>\n</soap:Envelope>"
headers = {
'content-type': "text/xml; charset=utf-8",
'host': "traindata.njtransit.com",
'soapaction': "http//microsoft.com/webservices/getTrainScheduleJSON"
}
response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
print(response.text)
I would greatly appreciate any help/insight.
You never want to set the "Host" header, this is will be done by requests.
The 404 is triggered by the wrong SOAPAction. There is a missing : after http.
The following snippet is working fine for me.
import requests
url = "http://traindata.njtransit.com:8090/NJTTrainData.asmx"
querystring = {"wsdl":""}
payload = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n <soap:Header>\n <UserCredentials xmlns=\"http://microsoft.com/webservices/\">\n <userName>---</userName>\n <password>---</password>\n </UserCredentials>\n </soap:Header>\n <soap:Body>\n <getTrainScheduleJSON xmlns=\"http://microsoft.com/webservices/\">\n <station>NY</station>\n </getTrainScheduleJSON>\n </soap:Body>\n</soap:Envelope>"
headers = {
'content-type': "text/xml; charset=utf-8",
'soapaction': 'http://microsoft.com/webservices/getTrainScheduleJSON'
}
response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
print response
print(response.text)