PhpStorm/WebStorm REST client editor: Using variables in JSON - phpstorm

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

Related

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.

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

Error uploading json file to a Cassandra DB through a Play application

I want to upload a json file to my cassandra DB by using a Play application/api.
My html view looks like this.
#main("Welcome to Play") {
<h1> Upload a JSON file </h1>
#helper.form(action = routes.Application.upload, 'enctype -> "text/json") {
<input type="file" name="jsonFile">
<p>
<input type="submit" value="Upload">
</p>
}}
I defined my upload method in the controller:
def upload = Action(parse.json) { request =>
val data = Json.arr(request.body)
sc.parallelize(Seq(data)).saveToCassandra("person", "user", SomeColumns("name", "age"))
Redirect("/index") }
The error message I get is
Bad Request: For request 'POST /upload' [Expecting text/json or application/json body]
Tried with enctype "application/json" as well and there was no difference.
If I try to print the content out I get Null.
The format of my file.json is:
{ "name": "alice", "age": 22}
{ "name": "bob", "age": 23}
Basically I can't seem to properly extract the content from the file.
See this section of the docs, about request body parsers:
https://www.playframework.com/documentation/2.5.x/ScalaBodyParsers#Choosing-an-explicit-body-parser
Basically, parse.json requires that you made a request with Content-Type of application/json. From the docs:
The json body parser will validate that the request has a Content-Type of application/json, and send back a 415 Unsupported Media Type response if the request doesn’t meet that expectation. Hence we don’t need to check again in our action code.
There is also another body parser that does not have this requirement. Again, from the docs:
This of course means that clients have to be well behaved, sending the correct Content-Type headers with their requests. If you want to be a little more relaxed, you can instead use tolerantJson, which will ignore the Content-Type and try to parse the body as json regardless:
def save = Action(parse.tolerantJson) { request =>
Ok("Got: " + (request.body \ "name").as[String])
}
So, you have two options here:
Use parse.json and set Content-Type to application/json
Use parse.tolerantJson
Besides that, you JSON file is not valid if the content is like this:
{ "name": "alice", "age": 22}
{ "name": "bob", "age": 23}
You can check it using JSONLint and I also recommend you to read the definition at json.org. You probably want to have a array like this:
[
{ "name": "alice", "age": 22},
{ "name": "bob", "age": 23}
]
After correct the format, you can either use parse.json or parse.tolerantJson.

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.

grails controller - POST json request

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!