我有一个AWS lambda函数,它有5个参数。其中一个参数是从PHP后端传递的Url,作为get参数。 url在传递时进行编码,参数作为JSONObject传递。请注意,我正在使用amazonaws sdk(com.amazonaws.util.json)的JSONObject。
在从中获取值之前,输入对象将转换为lambda函数中的JSONObject。由于url,转换为Json的输入时会出现问题。如果我传递一个字符串代替url,lambda工作得很好。
以下是相关代码和输出:
@Override
public String handleRequest(Object input, Context context) {
LambdaLogger logger = context.getLogger();
if (DEBUG)
logger.log("Starting LambdaFunction");
TwilioRestClient client = new TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);
try {
if (DEBUG)
logger.log("Input: " + input.toString());
JSONObject object = new JSONObject(input.toString());//Problem here
if (DEBUG)
logger.log("Object: " + object.toString());
String name = object.getString("name");
String message = object.getString("message");
String survey_url = object.getString("url");
String user_id = object.getString("user_id");
String number = object.getString("number");
if (DEBUG) {
logger.log("Name: " + name);
logger.log("Message: " + message);
logger.log("Survey Url: " + survey_url);
logger.log("User ID: " + user_id);
logger.log("Number: " + number);
}
// Other functionality
}
catch (JSONException e) {
logger.log("JSONException");
e.printStackTrace();
} catch (TwilioRestException e) {
logger.log("TwilioRestException");
e.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
return "End of SendSMSLambdaFunction";
}
从php调用 - > Lambda ::: https://xxx.amazonaws.com/prod/sendsmsapiresource?name=test&number=9618143233&url=http%3A%2F%2Fexample.com%2F&message=testmesage&user_id=1
从cloudwatch输出Lambda :::
Input: {name=test, number=9618143233, message=test, url=http://www.example.com, user_id=1}
JSONException
com.amazonaws.util.json.JSONException: Expected a ',' or '}' at 55 [character 56 line 1]
如何将url作为get参数传递给lambda函数?
答案 0 :(得分:0)
问题解决了,当我在php的lambda调用中为url添加了''。所以现在调用看起来像https://xxx.amazonaws.com/prod/sendsmsapiresource?name=test&number=9618143233&url=%27http%3A%2F%2Fexample.com%2F%27&message=testmesage&user_id=1
的调用,我对lambda的输入来自Input: {name=test, number=9618143233, message=test, url='http://www.example.com', user_id=1}