convert JSON input to form-data in AWS application gateway - amazon-web-services

Our old legacy APIs accept the data only in form-data format, but I am required to send my data as JSON in body of request. So, how I can convert my JSON (application/json) input to form data in AWS Application gateaway.
I have input parameter like this
{"key1": "val1", "key2": "val2"}
I tried many solution with template mapping and query string parameter but they didn't work for me maybe I am doing something wrong. Above configuration is fully supporting form-data.
Note: Due to some reason I don't want to change my legacy django code to handle JSON input instead of form data.

I would suggest looking at the structure of a raw POST form-data request. Then you should be able to structure the Integration Request to fit the form-data format using the right Content-Type and mapping template.
There is an example for GET and an example for POST on this page (with the caption "the HTTP request looks like this") https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Sending_and_retrieving_form_data

Related

Receiving unstructured data with Business Central web service/api?

I want to expose an endpoint in Business Central which can receive HTTP POST/PUT request and save content of that request into Blob, without any validation. I need that so I can receive json or xml data from external custom services.
I'm not sure how to achieve that since both OData and SOAP expect data to be in specific format and trigger data validation before I can even access POST-ed data.
I've been trying to play with Bound/Unbound actions but still can't figure out a way to access the content of the request. WebServiceActionContext is not well documented and I have not idea what I can or can't do with it. More about that: Microsoft documentation, Tutorial.
Any idea how to read content of a HTTP POST/PUT request is appreciated!
You could create a codeunit with a global procedure and then publish that as your web service.
Something like this:
codeunit 50000 MyUnstructuredWebService
{
procedure PostUnstructuredData(RequestId: Text; RequestContent: BigText);
begin
end;
}
Then you can handle the data directly or save it to a BLOB field in a table for later use.
The trick is to use BigText as the datatype for the parameter containing your actual content.

How to attach file to Postman environment?

How can I attach a file to the environment in order to launch the collection in the Postman collection runner? The file is attached to the form-data, and manually the request runs ok, but as soon as I run it in the collection runner, the runner doesn't see the file and the 500 Internal server error occurs. How can this issue be fixed?
Request BodySee the full detail in the link
While constructing requests, you’ll work frequently with the request body editor. Postman lets you send almost any kind of HTTP request. The body editor is divided into 4 areas and has different controls, depending on the body type.
Note about Headers: When you are sending requests through the HTTP protocol, your server might expect a Content-Type header. The Content-Type header allows the server to parse the body properly. For form-data and urlencoded body types, Postman automatically attaches the correct Content-Type header so you don’t have to set it. The raw mode header is set when you select the formatting type. If you manually use a Content-Type header, that value takes precedence over what Postman sets. Postman does not set any header type for the binary body type.
Form-data
form-data
multipart/form-data is the default encoding a web form uses to transfer data. This simulates filling a form on a website, and submitting it. The form-data editor lets you set key-value pairs (using the data editor for your data. You can attach files to a key as well. Note: due to restrictions of the HTML 5 spec, files are not stored in history or collections. You will need to select the file again the next time you send the request.
Uploading multiple files each with their own Content-Type is not supported yet.See the image here in the link

Returning binary body and http headers from an AWS lambda through API gateway

I have a lambda that needs to return a binary object and some http headers (e.g. content-type) through an api gateway (using lambda integration) OR redirect to another URL. In the binary support examples (e.g. https://aws.amazon.com/blogs/compute/binary-support-for-api-integrations-with-amazon-api-gateway/) the lambda only returns the (base64 of the) binary object (the image). In my case, I also need to return a status code and http headers (or something equivalent). I struggle with how I can make this work with binary support in api gateway.
The lambda returns a json on this form:
{
"statusCode": 200,
"headers": {
"content-type": "image/jpeg"
},
"body": "/9j/4AAQS...gLDAoKCAwZK",
"isBase64Encoded": true
}
In the integration response I add body mappings for image/jpeg (etc) of the form:
$input.json('$.body')
And header mapping for 'content-type' like so:
integration.response.body.headers['content-type']
I've tried many variations of the above, but the result is consistently
Execution failed due to configuration error: Unable to transform response
How do I transform the json from the lambda into a form that can be converted to binary by the api gateway, with http headers and all? Can I get more debug logging out of the api gateway to show more specific what it is unhappy with?
Is there perhaps a way to get more debug logging out of the api gateway?
I recently got this working after facing a similar problem.
In my case, I was missing two things:
First, I needed to change the list of types AWS will send to the upstream in the "Accept" header"
"x-amazon-apigateway-binary-media-types" : [
"image/jpeg"
]
Secondly, I needed to set the Integration Response to "Convert to binary (if needed)":
"contentHandling": "CONVERT_TO_BINARY"
See this answer for the details, and sample config.
I also found that I wasn't being patient enough. Whenever I deployed the API, I was checking immediately, instead of waiting a few minutes for the changes to propagate.
I tired returning binary data with base64 encoding, but I couldn't manage to return it from Lambda function through API Gateway.
Therefore, I decided to redirect the URL. I changed the Method Response to 302 and added "Location" Response header. I also deleted response code 200 from Integration Response, selected 302 as response code and mapped integration.response.body.location value with Location header. My Lambda code returned location of redirect URL in this format:
{"location":"www.google.com/"}
Hope this helps.
I've been struggling to do the same for days, but couldn't find any documentation to back up that it's possible, instead I found this
https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings-workflow.html
When converting a text payload to a binary blob, API Gateway assumes that the > text data is a Base64-encoded string and outputs the binary data as a
Base64-decoded blob. If the conversion fails, it returns a 500 response
indicating an API configuration error. You do not provide a mapping template
for such a conversion, although you must enable the passthrough behaviors on
the API.
When converting a binary payload to a text string, API Gateway always applies > a Base64 encoding on the binary data. You can define a mapping template for
such a payload, but can only access the Base64-encoded string in the mapping
template through $input.body, as shown in the following excerpt of an example > mapping template.
which sounds to me that mapping like this is only possible the other way around; what I ended up doing is that just return Base64Encoded binary string and hard code the content-type and cache- control in header mapping; for the complete implementation see my blog post https://mesfinmoges.com/dynamic-image-resizing-using-amazon-s3-aws-lambda-mazon-api-gateway-amazon-cloudfront/

How Qualtrics understands the response

I have implemented a web-service in qualtrics after a question block and the job is to do an operation and send a response back to my website after the survey.I don't know how qualtrics record these responses. Is there any logs section in qualtrics where we can see these responses? Can we deal with the responses in the qualtrics login?
If I'm understanding correctly, you're asking how responses are recorded in Qualtrics WebServices.
When a response comes in, Qualtrics parses the response as embedded data fields. Please note that the response has to be in json or xml encoding for Qualtrics to be able to properly parse it. When you test a webservice it gives you the option to set embedded data based on the response that comes back during testing.
Qualtrics automatically parses your response and assigns it to each parameter.
Either way work. (I didn't test in xml but, I assume it would work as well)
q1=answer1&q2=answer2&...
in JSON {"q1":"answer1", "q2":"answer2", ...)

How to send Queryparam with JSON value using Jersey client in unit test

I am trying to write a test case for a jersey resource using InMemory container provided by Jersey.
As my service method contains many multivalued parameters as filters, I opted to send all of those values as single JSON parameter, so that it will be easy to send a list of values for each filter.
When i send the JSON string using target("path").queryParam("filters", jsonString).request().get(); the call fails die to Jersey clients internal query builder, which is parsing the url and checking for path param templates in the url. Since the url contains my JSON with "{" in it, they are interpreted as path param.
If I try to encode the JSON using URLEncode.encode(jsonStr, "UTF-8"), the path param template issue is solved but white spaces in JSON are received by server as "+" as jersey client encoding URL one more time, but server decoding it only once.
If I make the Queryparam as post param test is working, but i don't want to use POST for just to retrieve data.
I can't post original code due to company policies.
My question is, is there any way to disable path template check in jersey clieny by setting custom builder.
A simpler solution would be to replace the '+' by '%20' as suggested here and here:
URLEncode.encode(jsonStr, "UTF-8").replaceAll("\\+", "%20");