I set up a POST ressource in API Gateway which invokes a Lamdba function, fine.
In the "integration request", for text/plain Content-type, I defined the following mapping :
#set($messageStr = $util.urlEncode($input.body))
{
"message" : "$messageStr"
}
Problem is : whenever I POST data with JSON reserved characters, the integration request mapping thows
{"message": "Could not process payload"}
Example : blablabla will be correctly passed through to lambda, not {blablabla nor a:e ...
This seems like that method urlEncode expects JSON data.
Any idea how to pass whatever body as pure text ?
Thanks
Related
For some reason, when I send JSON formatted data through Postman as raw text, I have no issues. When I send the exact same data through Postman as raw JSON (the difference should only be that the content-type header is application/json instead of application/text), I end up with my double quotes stripped and my strings switched to single quotes.
Original payload example (Postman sends this):
{ "id": "blahblahbloo", "time": "hammer" }
Unintended transformation (NextJS receives this):
{ id: 'blahblahbloo', time: 'hammer' }
To be clear, I get the exact same thing (which is what I expect) when I send as raw text via Postman:
// Postman sends this and NextJs receives this when set to raw text
{ "id": "blahblahbloo", "time": "hammer" }
I'm not explicitly doing anything to read the content-type and transform the data. My endpoints with this issue is a NextJS Dynamic Route: https://nextjs.org/docs/api-routes/dynamic-api-routes
Next.js API routes have a built-in bodyParser middleware that will parse the incoming request's body based on its Content-Type header.
From the API Middlewares documentation (emphasis is mine):
API routes provide built in middlewares which parse the incoming
request (req). Those middlewares are:
req.cookies - An object containing the cookies sent by the request. Defaults to {}
req.query - An object containing the query string. Defaults to {}
req.body - An object containing the body parsed by content-type, or null if no body was sent
Sending the payload as application/json will have the API route convert the req.body to a JavaScript object, hence striping the double-quotes.
While the bodyParser middleware is automatically enabled by default, you can disable it if you want to consume the body yourself.
// In the API route
export const config = {
api: {
bodyParser: false
}
}
I'm trying to build a REST API using Symfony 3.1 and the FOSRestBundle, FOSUserBundle and FOSOAuthServerBundle. I managed to achieve this following the guide at https://gist.github.com/tjamps/11d617a4b318d65ca583.
I'm now struggling at the authentication process. When I make a POST request to the server for authentication (to localhost:8000/oauth/v2/token) with the parameters encoded in json in the request body:
{
"grant_type": "password",
"client_id": "1_myveryverysecretkey",
"client_secret": "myveryverymostsecretkey",
"username": "theuser",
"password": "thepassword"
}
The additional HTTP Headers are the following:
Accept: application/json
Cache-Control: no-store, private
Connection: close
Content-Type: */json
The client in the db table oauth2_client has the "password" grant_type a:1:{i:0;s:8:"password";}, as suggested by the guide.
The server is accepting the request, but I always get the response
{"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}
Any suggestions what I am missing? Thanks!
I had the same problem. It seems fosOAuthBundle is not accepting json. if you send the query with form fields, it will work.
This is because FOSRestBundle uses a body listener which converts underscored keys to camel case. So the parameters that your OAuth2 server gets are not grant_type, but rather grantType, which it cannot process and so it gives you that error.
A solution for this would be to use a custom array normalizer on the body listener of fos rest.
really the FOSRestBundle Body Listener is the main 'cause' of this issue.
Array normalizer config
fos_rest:
body_listener:
array_normalizer: fos_rest.normalizer.camel_keys
it converts _ to camel case format.
The solution was remove it of my configuration by the moment.
calling again /oauth/v2/token endpoint:
{
"access_token": "NDBlZGViN2YwZGM5MTQ3ZTgwN2FhOGY4MDU4MTc1MTY2YzZmOThlMTdkM2JiZDJmMDVmNTg3MjU4N2JmODY3ZA",
"expires_in": 3600,
"token_type": "bearer",
"scope": null,
"refresh_token": "MDRiODllNjlhZWYxZjI5MjlhMzAxNGVhMDY5NjQxMmVmNDE5MzY3YzU0MGM0MDU1ZTVlY2Y2Zjg4ZTYyYzU3Mw"
}
I am looking to write middleware to modify response object that is generated by rest framework - JSON format.
I want to add to each JSON response some so called envelope that would wrap data object to something like:
{
"status" : "success",
"server" : "server name",
"source" : "cache", -- or "database"
"data" : ... original response from API ...
"errors" : [],
ect ....
}
I was thinking about creating middleware that would look for 'application/json' content and if yes capture response wrap it in that object but not react if other types 'application/text'
How I can access to content type in process_response method in middleware ?
And is it proper way to do such tasks ?
Your middleware's process_response method takes two arguments, request and response object.
In the method you can check the content type with response['Content-Type'].
I understand Dojo's XHR mechanism supports CORS but sets the X-Requested-With by default. I just have to set the headers = {"X-Requested-With":null }
So I am using dojo/request/xhr to call a Perl Web Service (Catalyst Framework)
xhr.get(url, {
handleAs: "json",
headers: {"X-Requested-With": null}
I'm using Fiddler and this is the JSON string:
{
"data" : [
{
"structure" : [
{
"name" : "State"
}
],
"dataSource" : [
{
"State" : "CA",
"Id" : 1
}
]
}
]
}
Fiddler returns an HTTP Result of 200 but I'm getting an error in dojo.then{}
"Unable to load http://Server:3000/state/ status: 0"
I also tried using dojo/request/script to call a Perl Web Service (Catalyst Framework)
script.get(url, {
jsonp: "callback"
}).then(...
But I get scripting error:
JavaScript critical error at line 2, column 11 in http://Server:3000/state/?callback=dojo_request_script_callbacks.dojo_request_script0\n\nSCRIPT1004: Expected ';'
It highlights the ":" after "data". I don't believe this ill-formed json string because my MVC controller method can call the Perl Server using WebClient's DownloadStringTaskAsync and return the value as a ContentString.
Any ideas??
If you're using CORS, you have to do more than just setting an X-Requested-With header. You also have to make sure the server sends the right headers, it had to send the Access-Control-Allow-Origin header and probably also the Access-Control-Request-Header header, for example:
Access-Control-Allow-Origin: *
Access-Control-Request-Header: x-requested-with
If you're using JSONP, your webservice must support JSONP, which means it's wrapping the entire result in a callback function.
I am developing in Worklight 5.0.6. I have created an HTTP adapter. On my server side, I have a php script that accepts a parameter from the URL, performs a check on the data using php, and then returns a JSON Object back.
When I go to the actual page, It returns data like this:
[{"SUCCESS": "888888888"}]
Now, when I run my HTTP Adapter, I get these results:
errors: Class Cast: java.lang.String cannot be cast to org.mozilla.javascript.Scriptable
I've tried to change the returned Content type to text, plain, JSON, json/application and none of those options work. How do I need to return the data from the server so that the HTTP adapter can read it or what changes do I need to make to the HTTP Adapter?
method : 'get',
returnedContentType : 'plain',
path : path,
headers: 'accepts: */*',
parameters : {
'myparam' : param,
}
};
return WL.Server.invokeHttp(input);
Wrap your json array in an object. Worklight can't handle returning arrays :(