Random Error From Node.Js and Lua HTTP Get? - node.js

So I'm trying to recieve data like the clients IP from a pipedream workflow using lua(HTTPGET). And so I have a custom body response in Node.JS with no errors. However, when I print the body response in Lua I get this unusual error. It prints the ip but then says A malformed number is near the clients ip.:
My Node.JS code for pipedream:
await $respond({
status: 200,
immediate: true,
body: `${steps.trigger.event.client_ip}`
})
There are no errors, its fine but heres my Lua code to get the response:
function GetServProtocal()
print(loadstring(game:HttpGet('https://enznhjjype22xb2.m.pipedream.net')))()
end
GetServProtocal()
Also, no errors but the output is weird:
-- I'm using 1.1.1.1 example ip for code so I don't expose my real one.
[string "1.1.1.1"]:1: malformed number near "1.1.1.1"

Remove loadstring from your code. The loadstring function expects some Lua code, which it will turn into an executable function and the string that is being returned by HttpGet is not a valid Lua code, hence the error you get.

Related

Python Client Rest API Invocation - Invalid character found in method name [{}POST]. HTTP method names must be tokens

Client
Python Version - 3.9,
Python Requests module version - 2.25
Server
Java 13,
Tomcat 9.
I have a Tomcat+Java based server exposing REST APIs. I am writing a client in python to consume those APIs. Everything is fine until I send empty body in POST request. It is a valid use case for us. If I send empty body I get 400 bad request error - Invalid character found in method name [{}POST]. HTTP method names must be tokens. If I send empty request from POSTMAN or Java or CURL it works fine, problem is only when I used python as a client.
Following is python snippet -
json_object={}
header = {'alias': 'A', 'Content-Type' : 'application/json', 'Content-Length' : '0'}
resp = requests.post(url, auth=(username, password), headers=header, json=json_object)
I tried using data as well instead of json param to send payload with not much of success.
I captured the wireshark dumps to undertand it further and found that, the request tomcat received is not as per RFC2616 (https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html). Especially the part -
Request-Line = Method SP Request-URI SP HTTP-Version CRLF
Because I could see in from wireshark dumps it looked like - {}POST MY-APP-URI HTTP/1.1
As we can see the empty body is getting prefixed with http-method, hence tomcat reports that as an error.
I then looked at python http library code -client.py. Following are relevant details -
File - client.py
Method - _send_output (starting at line # 1001) - It first sends the header at line #1010 and then the body somewhere down in the code. I thought(I could be wrong here) perhaps in this case header is way longer 310 bytes than body 2 bytes, so by the time complete header is sent on wire body is pushed and hence TCP frames are order in such a way that body appears first. To corroborate this I added a delay of 1 second just after sending header line#1011 and bingo, the error disappeared and it started working fine. Not sure if this is completely correct analysis, but can someone in the know can confirm or let me know how to fix this.

How do I return an error from route in express.js?

I want to return an error to the user whenever he sends invalid data to the server, eg. 8 digits instead of 10 in a phone number input.
You can stop the current node.js process by calling process.exit(1)
See docs.
EDIT:
If you want to keep the server running but simply return an error message from the route you can do something like this:
return res.status(400).send('Mobile number must be 10 digits long');
This sends HTTP code '400 Bad request' to the client.

file_get_contents() issues related to my SMS API intrigation

file_get_contents(http://smshorizon.co.in/api/sendsms.php?user=***&apikey=******&mobile=*****&message=Hello World&senderid=xxyy&type=txt)
Here in this SMS API when I use a message with space say "Hello World" then it return HTTP/1.1 400 Bad Request. But when I remove the space and instead I write HelloWorld it works fine.
What is the issue?
Why its happening (Problem):
URL is getting spitted because of space between messages and it will consider only part before space.
Solution:
You can use PHP inbuilt function urlencode();
Input : urlencode('Hello World');
Output : urlencode('Hello%20World');

Python3 - Error posting data to a stikked instance

I'm writing a Python 3 (3.5) script that will act as a simple command line interface to a user's stikked install. The API is pretty straight forward, and its documentation is available.
My post function is:
def submit_paste(paste):
global settings
data = {
'title': settings['title'],
'name': settings['author'],
'text': paste,
'lang': settings['language'],
'private': settings['private']
}
data = bytes(urllib.parse.urlencode(data).encode())
print(data)
handler = urllib.request.urlopen(settings['url'], data)
print(handler.read().decode('utf-8'))
When I run the script, I get the printed output of data, and the message returned from the API. The data encoding looks correct to me, and outputs:
b'private=0&text=hello%2C+world%0A&lang=text&title=Untitled&name=jacob'
As you can see, that contains the text= attribute, which is the only one actually required for the API call to successfully work. I've been able to successfully post to the API using curl as shown in that link.
The actual error produced by the API is:
Error: Missing paste text
Is the text attribute somehow being encoded incorrectly?
Turns out the problem wasn't with the post function, but with the URL. My virtual host automatically forwards http traffic to https. Apparently, Apache drops the post variables when it forwards.

How to response non-latin characters in AWS lambda?

Update Oct 12:
The issue is fixed now. See this post in aws forum for details.
I wrote a nodejs function simply respond with some Chinese characters. But it respond with wrong characters.
exports.handler = function(event, context) {
context.succeed('Hello 世界!');
};
The function result becomes:
"Hello ������������!"
I came across this problem when I wrote a function to parse some Chinese websites and retrieve their page titles. I manage to convert them into utf-8 (I used needle for the request), and console.log(title) correctly display those Chinese characters. But the result from context.succeed() shows up like the example above. What should I do to deal with these non-latin characters when responding the result?
From AWS Support (August 10, 2015):
Thank you for reaching out AWS Support with your question about
Lambda and UTF-8.
We are presently researching this issue as other customers have
brought this to our attention. There is no eta on when this will be
resolved or if this is something we can resolve.
As in AWS Documentation - Programming Model (Node.js):
Indicates the Lambda function execution and all callbacks completed
successfully. Here's the general syntax:
context.succeed (Object result);
where
Object result – provides the result of the function execution. The
result provided must be JSON.stringify compatible. This parameter is
optional. You can call this method without any parameters (succeed())
or pass a null value (succeed(null)). If AWS Lambda fails to stringify
or encounters another error, an unhandled error is thrown, with the
X-Amz-Function-Error response header set to Unhandled.
So, you can't get the result like this:
Hello 世界!
Because, the string Hello 世界! will be encoded as JSON. So, it will return:
"Hello 世界!"
If you see in the browser using AWS Lambda Console:
"Hello ������!"
Actually it's a valid JSON, you just need to decode it first and then process it.
Try to run this:
exports.handler = function (event, context) {
var jsonStr = JSON.stringify('Hello 世界!');
console.log(jsonStr);
console.log(JSON.parse(jsonStr));
context.succeed('Hello 世界!');
};
The log result will be:
2015-08-07T12:49:54.888Z 12345678-90ab-cdef-1234-567890abcdef "Hello 世界!"
2015-08-07T12:49:54.889Z 12345678-90ab-cdef-1234-567890abcdef Hello 世界!
After you decode it, you can get back your original string.

Resources