Meteor iron:router prepare request.body when JSON - json

Currently I play around with iron:router's solution for a restful API. For this I use the .put, .get ... methods which are iron:router has implemented.
This is my example I work with:
Router.route('/api', {where:'server'})
.put(function(){
var req;
req = this.request;
console.log(req.body);
this.response.end('PUT finished.');
});
When I execute the following I will get the expected response (PUT finished):
curl -XPUT "http://localhost:4000/api " -d'{"name": "timo"}'
But the console.log(req.body) returns a strange value converted to an object.
The returned value is:
{
'{"name": "timo"}\n' : ''
}
It seems that iron:router trys to convert the body into an object but did not recognized that the given request string is a valid JSON string.
Is there smth I did wrong ? I did not find anything helpful yet to prepare iron:router that the given request body is still JSON.
Maybe its a better solution not to tell iron:router that the given request is a JSON and instead to tell iron:router that it shouldn't do anything so I can convert the JSON string by myself ?

You didn't specify the content type in your curl request. Try this instead:
curl -XPUT "http://localhost:4000/api " -d'{"name": "timo"}' -H "content-type: application/json"
With that it works for me:
I20150522-09:59:08.527(-7)? Request { name: 'timo' }
(and without it doesn't).

Related

HTTP Header response accept: application/x-pblist

I have some Issue with an API. I wanna fetch data over an curl command wich is working fine. But I can't handle the return type. The default header-accept look like.
-H 'Accept: application/x-bplist'
What is this fpr a return type and how can i convert it in json.
Some other request with no Accept brings back something like this (this is no json!):
{
"data1" = ();
"data2" = {
"value1" = "a...";
};
}
The () stands for an array {}; seems to be an hash, but what for a datatype is this and how to easily convert it in json?
giving an -H "Accept: application/json" will throw an error.
If you have an Idea, thanks
regards
Mat

apigee 127 - JSON is escaped when ran in mock mode

I'm guessing I've missed something silly so I apologize in advance.
I'm trying to run the example found at the following URL in mock mode: https://github.com/apigee-127/a127-samples/blob/master/weather-basic/api/swagger/swagger.yaml
The JSON response looks like it's being escaped and it's causing the response validator to fail:
{
"message": "Response validation failed: invalid content type (text/plain). These are valid: application/json",
"failedValidation": true,
"originalResponse": "{\"base\":\"Sample text\",\"clouds\":{\"all\":1},\"cod\":1,\"coord\":{\"lat\":1,\"lon\":1},\"dt\":1,\"id\":1,\"main\":{\"humidity\":1,\"pressure\":1,\"temp_max\":1,\"temp_min\":1,\"temp\":1},\"name\":\"Sample text\",\"sys\":{\"country\":\"Sample text\",\"id\":1,\"message\":1,\"sunrise\":1,\"sunset\":1,\"type\":1},\"weather\":[{\"description\":\"Sample text\",\"icon\":\"Sample text\",\"id\":1,\"main\":\"Sample text\"}],\"wind\":{\"deg\":1,\"speed\":1}}"
}
Any ideas would be awesome!
You need to send something like this res.json({});
Send as json object don't stringify your result object ,
var result = {name:"asdf",id:1212}
res.json(result);

Shopify integration: trouble on making authenticated requests

Sorry for cross posting this issue here in SO
So I follow the "https://docs.shopify.com/api/authentication/oauth"; guide and successfully proceed to "Making authenticated requests" part, then I stuck at there. Here is my code (in Java):
String payload = "{\"script_tag\":{\"src\":\"http:\\/\\/localhost:8080\\/js\\/shopify.js\",\"event\":\"onload\"}}";
String url = "https://pixolut-shopify-test.myshopify.com/admin/script_tags.json";
HttpPost post = new HttpPost(url);
post.setHeader("X-Shopify-Access-Token", accessToken);
post.setEntity(new StringEntity(payload, ContentType.APPLICATION_JSON));
HttpResponse resp = HttpClientBuilder.create().build().execute(post);
StatusLine statusLine = resp.getStatusLine();
if (statusLine.getStatusCode() != 200) {
throw new RuntimeException("Error inject script tag: %s", statusLine.getReasonPhrase());
}
I am using apache httpclient (v4.3.1) to post my request to Shopify. The problem I've found is I always get HTTP/1.1 422 Unprocessable Entity, I don't know where I am wrong.
If I use postman to test with exactly the same payload, url and access token, I get the following response:
{
"errors": {
"script_tag": "Required parameter missing or invalid"
}
}
Anyone can help?
Update
I got content of the 422 response:
{"errors":{"src":["is invalid"]}}
I had the same problem, using curl. The missing ingredient was setting the Content-Type for the request:
curl -H "X-Shopify-Access-Token: {token}" -H "Content-Type: application/json" -d "{\"script_tag\":{\"event\":\"onload\",\"src\":\"{script_uri}\"}}" https://{shop}.myshopify.com/admin/script_tags.json
You should be able to do the same with Postman: https://www.getpostman.com/docs/requests#headers
I got a similar error running the shopify api natively without a wrapper.
I ended up using a node module that helped. I know you're writing in Java so not sure if they have a similar wrapper.
It might help to take a look at how they implement pinging the shopify api in node.
https://github.com/christophergregory/shopify-node-api

NoJson found on an http request on play 2.3.2

i'm writing a Play 2.3.2 application using Scala.
In my controller i had a method which get the json object from the request.
The implementation is like the following:
def firstTags = Action.async { request =>
def elaborate(n: Int): Future[Result] = {//some implementation}
//get the json Object
val jsonObject = request.body.asJson
//parse the json object
jsonObject match {
case Some(json) => json \ "number" match {
case x: JsNumber => elaborate(x.as[Int])
case _ => Future{BadRequest}
}
case None => Future{BadRequest("Need a Json")}
}
}
In my route files i had:
GET /recommendation/statistic/first #recommendationsystem.controllers.manager.StatisticsController.firstTags
When i try to call the method with the curl i get always a "Need a Json" response.
I call my server with curl like the following:
curl -H "Accept: application/json" -H "Content-Type: application/json" -d '{"number": 3}' -X GET http://localhost:9000/recommendation/statistic/first
What's wrong??
GET shouldn't have body. look at HTTP GET with request body.
POST method is not only for modify the server state, but also to process data.
From RFC 2616 - HTTP/1.1:
The POST method is used to request that the origin server accept the
entity enclosed in the request as a new subordinate of the resource
identified by the Request-URI in the Request-Line. POST is designed
to allow a uniform method to cover the following functions:
Annotation of existing resources;
Posting a message to a bulletin board, newsgroup, mailing list,
or similar group of articles;
Providing a block of data, such as the result of submitting a
form, to a data-handling process;
Extending a database through an append operation.
The actual function performed by the POST method is determined by the
server and is usually dependent on the Request-URI. [...]

Posting JSON with curl to Grails JSON REST API plugin

I have a Grails app running on 1.3.7 with the json-rest-api plugin version 1.0.8 installed. I'm trying to post data from the command line using curl in order to create a new instance of a domain class, but cannot seem to figure out the correct way to format the JSON so that the parser is satisfied. I can't find any documentation either that would conclusively describe how the data is supposed to be formatted.
The domain class is defined like this:
package foo
class Foo {
static expose = 'foo'
String name
static constraints = {
name(inList: ['xyzzy', 'quux'])
}
}
Given the above, this doesn't work:
$ curl -X POST --header "Content-Type: application/json" --data-urlencode '{"name":"xyzzy"}' http://myapp/api/foo
The app returns a 500 error code and the content of the reply is
{"success":false,"message":"Property [name] of class [class foo.Foo] cannot be null"}
What kind of data does the API expect me to send?
Try the following format:
{ data: { name: "Blabla" } }
This isn't specific to the json-rest API. I've only ever used the built in RESTful capabilities of Grails.
You need your URL Mapping so that parseRequest = true. You have to include the class in your JSON:
{"class":"Foo", "name":"blabla"}
In your controller, you do this...
def fooInstance = new Foo(params['foo'])
And your gold.