grails controller - POST json request - json

I have very simple controller with the following method:
def createNewWidgetVersion() {
println request.JSON
render status: OK
}
I'm sending the following request:
POST /CMSAdmin/widgetVersion/createNewWidgetVersion HTTP/1.1 Host:
localhost:8080
Content-Type: application/json
Cache-Control: no-cache
{ "htmlText":"33", "javaScript":
"document.getElementById('ffsdfdsfsfsfsd')", "comments":"SUCCESS",
"widgetId": "1", "type": "LIVE" }
and when debugging, IDE shows that request.JSON is empty. Controller doesn't extend RestfullController but I think it doesn't have to in this particular siutation. Could you please give a hint what I'm doing wrong? Spent few hours dealing with this. Thanks!
UPD1
I added the following to UrlMappings.groovy:
"/widgetVersion/"(controller: "widgetVersion", parseRequest:true){
action = [POST:'createNewWidgetVersion']
}
is there any way to avoid this? Thanks!

Related

PhpStorm/WebStorm REST client editor: Using variables in JSON

I cannot use variables from http-client.env.json within the body as the following
POST {{host}}/{{locale}}/foo
Accept: application/json
Content-Type: application/json
{
"email": {{email}},
"password": {{password}}
}
so the string interpolation {{email}} is not being recognized for some reason as if it's not supported yet, and whenever I'm changing my code to hardly coded values as the following snippet things will be just fine!
POST {{host}}/{{locale}}/foo
Accept: application/json
Content-Type: application/json
{
"email": "info#example.com",
"password": "saynomore"
}
And obviously extracting the body into its own external JSON file is not an option.
Check documentation
https://www.jetbrains.com/help/pycharm/http-client-in-product-code-editor.html
https://www.jetbrains.com/help/pycharm/exploring-http-syntax.html
Preferable option json.env file, you can autogenerate it https://www.jetbrains.com/help/pycharm/exploring-http-syntax.html#c3cb9d37

HTTP 406 not acceptable response header fiddler

I am using fiddler 4 as a frond-end service for elasticsearch.
I'm trying to store a document into index using API following this.
Here's my sample json as request body:
{
"fruit": "Apple",
"size": "Large",
"color": "Red"
}
API : http://localhost:9200/food/fruit
method :POST HTTP/1.1
Here's the response:
As mentioned in their doc., "Starting from Elasticsearch 6.0, all REST requests that include a body must also provide the correct content-type for that body." adding content-type: application/json in the request solved the issue.

Parsing AWS SNS notification in .net core 1.0

I have a VisualStudio17 serverless application project and am using .net core Web Api.
I want to confirm my SNS subscription, but I have a problem that AWS sends the POST request with header content-type set to text/plain; charset=UTF-8 while body is JSON.
Here's an example request from their documentation:
POST / HTTP/1.1
x-amz-sns-message-type: Notification
x-amz-sns-message-id: da41e39f-ea4d-435a-b922-c6aae3915ebe
x-amz-sns-topic-arn: arn:aws:sns:us-west-2:123456789012:MyTopic
x-amz-sns-subscription-arn: arn:aws:sns:us-west-2:123456789012:MyTopic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55
Content-Length: 761
Content-Type: text/plain; charset=UTF-8
Host: ec2-50-17-44-49.compute-1.amazonaws.com
Connection: Keep-Alive
User-Agent: Amazon Simple Notification Service Agent
{
"Type" : "Notification",
"MessageId" : "da41e39f-ea4d-435a-b922-c6aae3915ebe",
"TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic",
"Subject" : "test",
"Message" : "test message",
"Timestamp" : "2012-04-25T21:49:25.719Z",
"SignatureVersion" : "1",
"Signature" : "EXAMPLElDMXvB8r9R83tGoNn0ecwd5UjllzsvSvbItzfaMpN2nk5HVSw7XnOn/49IkxDKz8YrlH2qJXj2iZB0Zo2O71c4qQk1fMUDi3LGpij7RCW7AW9vYYsSqIKRnFS94ilu7NFhUzLiieYr4BKHpdTmdD6c0esKEYBpabxDSc=",
"SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem",
"UnsubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:123456789012:MyTopic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55"
}
Content-type: text, body JSON. This makes it parsing quite difficult, and a simple
public void Post([FromBody] string t) // or dynamic t for the matter
doesn't work and throws the Request was short circuited at action filter 'Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter'. exception.
Am I missing something? Why are they doing this and how do I work with this?
I made it work like I described in this answer, by adding text/plain to formats that JsonInputFormatter should format.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(config =>
{
foreach (var formatter in config.InputFormatters)
{
if (formatter.GetType() == typeof(JsonInputFormatter))
((JsonInputFormatter)formatter).SupportedMediaTypes.Add(
MediaTypeHeaderValue.Parse("text/plain"));
}
});
...
}

FOSOAuthServerBundle: Invalid grant_type parameter or parameter missing

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"
}

Multipart upload to Google Bigquery

I'm trying to formulate a body for my load job (multipart) into Bigquery. Following the example on https://cloud.google.com/bigquery/loading-data-post-request#multipart:
-foo_bar_baz
Content-Type: application/json; charset=UTF-8
{
"configuration": {
"load": {
"sourceFormat": "NEWLINE_DELIMITED_JSON",
"schema": {
"fields": [
{"name":"word","type":"STRING"},
{"name":"word_count","type":"INTEGER"},
{"name":"corpus","type":"STRING"},
{"name":"corpus_date","type":"INTEGER"}
]
},
"destinationTable": {
"projectId":"myproject",
"datasetId":"mydataset",
"tableId":"mytable"
}
}
}
}
--foo_bar_baz
Content-Type: */*
[MY-JSON-DATA]
--foo_bar_baz--
But I get the message from google saying:
"message": "Job configuration must contain exactly one job-specific configuration object (e.g., query, load, extract, spreadsheetExtract), but there were 0: "
If I look at the docs at https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.load it seems that the sourceUris[] is required but the example doesn't include it. My data is not in the google cloud storage but in the request. Why doesn't it detect my load details?
Thanks for any help. //Leif
Based on the message - looks like you are missing below
Content-Type: multipart/related; boundary="foo_bar_baz"
You were right. I had an extra space in my header for Content-Type, I saw it using fiddler and it looked like this:
`Content-Type : multipart/related; boundary="foo_bar_baz"
Thanks for your support.