Convert String to JSON AWS API Gateway - aws-api-gateway

I have a program that is trying to send a string for example http//:somewebsite.com using POST to my Lambda function via the AWS API Gateway. I know the API gateway only accepts JSON to be sent to Lambda, however, is there a way I can transform the string to JSON to be sent to Lambda within the API Gateway?

If you want to POST just url then please format your json like below,
{"Url" : "http//:somewebsite.com"}

Related

How can I put header parameter in a Url?

I want to get data from API. So first I want to get Json response from the browser. I Usually do it by specifying a query to pass the API key. But this time the API key is passed using header parameter instead of a query. How can I pass the API into a header parameter?
This is the base Url
"https://api-nba-v1.p.rapidapi.com"
This is the header parameter:
x-rapidapi-key
You can try to issue this request first using some rest client like postman or insomnia.

AWS API Gateway & Lambda Function for SOAP / REST Conversion

I have a legacy application which currently utilises a SOAP interface to invoke functions against.
I wish to serve this interface to a website, with it being preferential to be done using REST with a JSON scheme.
I wish to get feedback and validate my thinking for the solution to this problem as described below:
In order to do the transformation from SOAP (XML) <--> REST (JSON) I am planning on using the following components:
Amazon API Gateway: Receives a POST request from my website containing basic payload data.
Lambda Function: API Gateway invokes an AWS lambda function which is contains a soap client package. The function handles any necessary transformations and mappings and it is the Lambda function which issues the request to my legacy SOAP service.
SOAP service: Receives request, executed required function and returns XML response
Lambda Function: Receives response, parses any required data / handles any errors and returns the response call back to the API Gateway.
API Gateway: Returns result of Lamdba function invocation.
A few questions:
I am unsure whether it is the Lambda function which carries out the request directly to my SOAP service, I am assuming so as this is just an encapsulated function. Is this correct?
Does the Lambda function return the response back to the gateway request? I believe this is correct as the function can execute synchronously so will still have the context to return the response back.
Do you generally handle any authentication checks within the API Gateway for the initial inbound request or does the request in its entirety get passed down to the Lambda function where I would then conduct and execute any auth policies/checks?
For Question 1 & 2:
Yes, it is correct to do the SOAP request and transformation in the Lambda function.
For Question 3: To handle authentication at API Gateway, you can use a separate Lambda function called Custom Authorizer. You can also do authorization for API Gateway endpoints in Custom Authorizer Lambda function.

SoftLayer Rest API attachDiskImage call throwing error

I am trying with the below one it always throws "You must provide a valid portable storage volume id.". I have replaced my virtualGuestId,imageId,user name and apikey in below curl.
curl "https://[username]:[apiKey]#api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest/[virtualGuestId]/attachDiskImage.json?imageId=[imageId]"
Please let me know the correct API to use to load an disk image and how to fetch the details of disk image.
I recomend you to take a look at these links:
https://sldn.softlayer.com/blog/bpotter/more-softlayer-rest-api-examples
https://sldn.softlayer.com/article/REST
Well the method attachDiskImage has as parameters the "imageId" of the portable storage as you can see in documentation:
http://sldn.softlayer.com/reference/services/SoftLayer_Virtual_Guest/attachDiskImage
The parameters must be sent using the POST method and you need to send those parameters in a payload with a JSON format, so your request should be changed to this one:
curl -X POST -d '{"parameters":[$IMAGEID]}' https://$SLUSERNAME:$SLAPIKEY#api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest/$VIRTUALGUESTID/attachDiskImage.json
Note: replace $IMAGEID, $SLUSERNAME, $SLAPIKEY and $VIRTUALGUESTID
Regards

Accepting api keys in HTTP headers or JSON POST data

I have an HTTP JSON endpoint localhost:8000/resource/ that takes JSON data as input and returns JSON as output. I am now adding API Key based authorization to the endpoint. There are 2 ways to accept the API Key at this endpoint:
Method A: In the request headers
Example python code:
import requests
headers = {
'API-Key': '<my-api-key>',
}
r = requests.post('http://localhost:8000/resource/',
json={'input': <value>},
headers=headers)
Method B: In the JSON data itself
Example python code:
import requests
r = requests.post('http://localhost:8000/resource/',
json={'input': <value>, 'API-Key': '<my-api-key>'},)
I usually notice Method A being adopted. Is there anything wrong with latter approach in which the API key is passed along with the other json data?
I think it has to do with clarity, the API Key isn't really relevant to the input, it's just a form of authorization.
Large frameworks that deal with routing and such are able to filter based on specific headers, and it would be cumbersome to filter based off of a specific form of input in the request body that would require user to intervene and obtain that value for it. Headers are simpler, and suffice for simple data that would fit in a hash-table.

Sending rest api routes to client

I'm looking for a better way to structure my rest client/rest api code. Currently, I have my rest api with routes like:
get /book
The literal string "/book" is defined on the server side, and on the client side, I have to use the same literal string exactly.
What I would like to do is, have a file serverside which has all of my routes in it, and send it to the client as json, which the client uses for routing.
The basic workflow would be:
Start the server
When the client connects, make a get request for the routes object
return an object like this:
{
getBook:"/book",
createBook:"/book/create"
}
that way my client can use the object returned for the routes instead of hard coding them.
Is this a good idea?