I try to send webhook alerts to wunderbit (crypto trading platform), but i receive
<Response 403> error. Please help.
My code:
import requests
target_url= "https://wundertrading.com/bot/exchange"
message = "My open long command(generated by wunderbit)"
my_request = requests.get(target_url, data = message, headers = {"Content-Type": "text/plain"})
print(my_request)
were you trying to send a signal from a custom server?
Only signals from TradingView were allowed previously. Now you can send a custom signal, but you will need to whitelist your IP first. see WunderTrading documentation
Related
I have a system that sends a pin entry request to a client for payment processing. If client accepts payment and enters PIN, I get below results on my endpoint
I have tried to run the code below
response = requests.get('https://end9m3so3m5u9.x.pipedream.net/')
print(response.text)
print(response.status_code)
How do I access the ResultCode for further processing in my views.py? Please advice.
EDIT
Sorry if I wasn't clear. I am working with an API and I have an callback URL. Here's how it works:
Client makes a purchase
I inform the API of the Client's purchase and the price
API sends a PIN request notification to Client's phone
If client accepts transaction/declines transaction, API sends this (below) POST request/POST data (forgive me if the term is wrong, I'm new to this) to my endpoint url
I need to obtain the ResponseCode below to know whether to proceed with the purchase/ decline it.
I have tried below code which doesn't work as i want:
response = requests.get('https://end9m3so3m5u9.x.pipedream.net/')
print(response.text)
print(response.status_code)
print(response.json())
print(response.content)
Please advice.
Are you using requests libray?
If so, try this:
response = requests.get('https://end9m3so3m5u9.x.pipedream.net/')
print(response.content)
response = requests.get('https://end9m3so3m5u9.x.pipedream.net/')
print(response.json())
UPDATE:
See how it works in my python shell:
I am trying send the query and get the response message (data) through modbus using python with the help of pymodbus module.
response=client.read_holding_registers(19200,126,unit=135)
print(response)
The response that is being printed is the request message sent and the process status. I am interested in reading the data sent from the slave and not the process status. Can you please help me with this?
You should use the .registers
Try the following procedure:
response = client.read_holding_registers(19200, 126, unit=135)
if not response.isError():
print(response.registers)
else:
# Handle Error
[NOTE]:
.isError() method is for pymodbus 1.4.0 and above.
I am trying to update an already saved form on a system using HTTP requests. Due to the server configuration for the third party app we use, updating by POST requires sending a fully filled out payload every single time.
I want to get round this by recovering the form data already present on the server and converting it into a dictionary. Then changing any values I need and reposting to make changes sever side.
The application we use sends a POST request when the save button is clicked for a particular form.
Here I send a post request with no payload.
[This simulates pressing the save button and is also the point where dev tools shows me a the payload I want to capture]
post_test = self.session.post(url_to_retrieve_from)
I thought that now I should be able to print the output, which should resemble what Google Dev tools Form data captures.
print(post_test.text)
This just gives me html found on the webpage.
If Dev Tools can get this from the server then I should also be able to?
Example of Data I am trying to get via requests:
Form Data
If Dev Tools can get this from the server then I should also be able to?
Yes, of course. In requests you pass form data in data keyword:
import requests
url = 'http://www.example.com'
data = {
'name': 'value',
}
response = requests.post(url, data=data)
You can get the data you sent with a request from the response in this way:
import requests
response = requests.post('http://your_url', data=data) # send request
body = response.request.body
parsed_data = dict(data.split('=') for data in body.split('&')) # parse request body
Here you can find more information about data argument
In the documentation, in the class requests.Response we can find the attribute:
request = None
The PreparedRequest object to which this is a response.
In requests.PreparedRequest class we can read:
body = None
request body to send to the server.
I have a Django App that accepts messages from a remote device as a POST message.
This fits well with Django's framework! I used the generic View class (from django.views import View) and defined my own POST function.
But the remote device requires a special reply that I cannot generate in Django (yet). So, I use the Requests library to re-create the POST message and send it up to the manufacturer's cloud server.
That server processes the data, and responds with the special message in the body. Idealy, the entire HTML response message should go back to the remote device. If it does not get a valid reply, it will re-send the message. Which would be annoying!
I've been googling, but am having trouble getting a clear picture on how to either:
(a): Reply back in Django with the Requests.response object without any edits.
(b): Build a Django response and send it back.
Actually, I think I do know how to do (b), but its work. I would rather do (a) if its possible.
Thanks in Advance!
Rich.
Thanks for the comments and questions!
The perils of late night programming: you might over-think something, or miss the obvious. I was so focused on finding a way to return the request.response without any changes/edits I did not even sketch out what option (b) would be.
Well, it turns out its pretty simple:
s = Session()
# Populate POST to cloud with data from remote device request:
req = Request('POST', url, data=data, headers=headers)
prepped = req.prepare()
timeout = 10
retries = 3
while retries > 0:
try:
logger.debug("POST data to remote host")
resp = s.send(prepped, timeout=timeout)
break
except:
logger.debug("remote host connection failed, retry")
retries -= 1
logger.debug("retries left: %d", retries)
time.sleep(.3)
if retries == 0:
pass # There isn't anything I can do if this fails repeatedly...
# Build reply to remote device:
r = HttpResponse(resp.content,
content_type = resp.headers['Content-Type'],
status = resp.status_code,
reason = resp.reason,
)
r['Server'] = resp.headers['Server']
r['Connection'] = resp.headers['Connection']
logger.debug("Returning Server response to remote device")
return r
The Session "s" allows one to use "prepped" and "send", which allows one to monkey with the request object before its sent, and to re-try the send. I think at least some of it can be removed in a refactor; making this process even simpler.
There are 3 HTTP object at play here:
"req" is the POST I send up to the cloud server to get back a special (encrypted) reply.
"resp" is the reply back from the cloud server. The body (.content) contains the special reply.
"r" is the Django HTTP response I need to send back to the remote device that started this ball rolling by POSTing data to my view.
Its pretty simple to populate the response with the data, and set headers to the values returned by the cloud server.
I know this works because the remote device does not POST the same data twice! If there was a mistake anyplace in this process, it would re-send the same data over and over. I copied the While/try loop from a Socket repeater module. I don't know if that is really applicable to HTTP. I have been testing this on live hardware for over 48 hours and so far it has never failed. Timeouts are a question mark too, in that I know the remote device and cloud server have strict limits. So if there is an error in my "repeater", re-trying may not work if the process takes too long. It might be better to just discard/give up on the current POST. And wait for the remote device to re-try. Sorry, refactoring out loud...
I have a Wechat subscribe page, and I set up in https://admin.wechat.com/ a token as well as the webhook URL of my server myserver.com/wechat/webhook
My server Python code authenticates Wechat server with signature check, nonce and timestamp and I'm able to receive messages from Wechat to my webhook.
But I can't send messages from my server to Wechat, I'm using the following code and the token set in the admin console as previously, and following the previous docs:
http://admin.wechat.com/wiki/index.php?title=Customer_Service_Messages
# Parse the received WeChat message
message = xmltodict.parse(message)
content = message['xml']['Content']
fromUser = message['xml']['FromUserName']
toUser = message['xml']['ToUserName']
createdTime = message['xml']['CreateTime']
# reply to message
post_data = {
"touser": fromUser,
"msgtype": "text",
"text":
{
"content": "Thanks for your message"
}
}
api_url = 'https://api.wechat.com/cgi-bin/message/custom/send?access_token=' + token
response = requests.post(api_url, data=post_data)
content = response.content
This is the error that I'm getting:
{"errcode":40001,"errmsg":"invalid credential, access_token is invalid or not latest hint: [udQ9ka0880vr32!]"}
What token am I supposed to use? Or what could be the error? Thanks
I think it might be a permission problem... In my Wechat page admin console, it says I have Permission obtained for Auto-reply, but not for Service API. Anyone can confirm this?
Indeed I tested successfully instantly replying to POST messages with an http response (Auto-reply or callback) http://admin.wechat.com/wiki/index.php?title=Callback_Messages
And the Service API seems to not be working (which was the purpose of my question) http://admin.wechat.com/wiki/index.php?title=Customer_Service_Messages