Quantcast
Channel: Tutorial - Embarcadero Community
Viewing all articles
Browse latest Browse all 503

Use C++Builder, connect to AWS API Gateway+(Node.js)Lambda with JSON[JAPAN]

$
0
0

Use C++Builder, connect to AWS API Gateway+Lambda with JSON.

Inside of AWS Lambda echoes the contents of post JSON.

/////
exports.handler = (event, context, callback) => {
    callback(null, event);
};

The function name is "test_function". And save it.

Make this Lambda"test_function" accessible from the Internet using Amazon API Gateway.

////
	TJSONArray*    __fastcall EchoStrings(TStrings* ss);

It seems of member function of DataSnap sample"EchoString()".


In TButton to make such Form, to call the EchoStrings(TStrings* ss).

////
TStringStream* __fastcall TForm1::str_stream_to_json(TStrings* ss)
{
	TStringStream* ms{nullptr};
	auto ja = new TJSONArray();
	int i{0};
	try
	{
		try
		{
			for (auto line_: ss)
			{
				ja->Add(new TJSONObject(new TJSONPair(
					Format("value_", ARRAYOFCONST((i)) ), line_) ));
				i++;
			}
		}
		catch(Exception& e1){}
		ms = new TStringStream(ja->ToJSON());
	}
	__finally
	{
		delete ja;
	}
	return ms;
}

TJSONArray* __fastcall TForm1::EchoStrings(TStrings* ss)
{
	std::unique_ptr<TNetHTTPClient> net_cliant_{new TNetHTTPClient(nullptr)};
	auto ms = str_stream_to_json(ss);
	_di_IHTTPResponse http_;
	net_cliant_->ContentType = "application/json";
	if (ms != nullptr)
	{
		try
		{
			http_ = net_cliant_->Post( "https://******/test-function?a=1",
				ms);
			std::unique_ptr<TStringStream> ms1 = std::make_unique<TStringStream>(static_cast<UnicodeString>(L""), TEncoding::UTF8, true);
			ms1->LoadFromStream(http_->ContentStream);
			ms1->Position = 0;

			return static_cast<TJSONArray*>((
				static_cast<TJSONObject*>(TJSONObject::ParseJSONValue(ms1->DataString))
				)->GetValue("body-json"));
		}
		catch(Exception& e1)
		{
			return nullptr;
        }
	}
	else
        return nullptr;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
	for (auto value_: EchoStrings(Memo1->Lines)) {
		Memo1->Lines->Append(
			static_cast<TJSONObject* >(value_)->GetValue("value_")->Value());
	}
}

Return contents written in Memo1 as TStringStream.

Reply in JSON like this.This is data returned by the Amazon API Gateway.

{
    "body-json": [
        {
            "value_": "English"
        },
        {
            "value_": "日本語"
        },
        {
            "value_": "русский"
        },
        {
            "value_": "한국"
        }
    ],
    "context": {
        "user-arn": ""
    },
    "params": {
            "X-Forwarded-Proto": "https"
        },
    "stage-variables": {}
}

 


Viewing all articles
Browse latest Browse all 503


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>