Search code examples
javascriptjsonmeteorhttp-headersiron-router

meteor iron-router header type not json


I am trying to use meteor together with iron-router for a simple RESTful API and I have verified that when I do a POST to my api if the header type is set to 'Content-Type: application/json' then I can parse the body of the http request using var thisVar = this.request.body.myKey, however if I don't specify the Content-Type of course the default is application/x-www-form-urlencoded and the body doesn't get parsed as json.

My question is then, how can I 'convert' my content to JSON as it really is JSON. Now, most of you might think "why not just change the content-type to json in the POST request". Well, the answer is that I do not control the POST, this is a 3rd party software. I could add an enhancement request, but that will take months and may not ever happen.

So, therefore my question again is: Within my iron-router Can I 'convert' the content-type of my body to json so that I can parse it easily using the format var thisVar = this.request.body.myKey

EDIT

Let me clarify, I already have a basic working RESTful API working with iron-router. When I use curl and POST with some json data and specify the content-type is json like so curl -XPOST -H "Content-Type:application/json" -d"{\"myKey\":\"myValue\"}" http://localhost:3000/api/insert then in my route for handling POSTs I can parse the body with var thisVar = this.request.body.myKey and then insert into my mongodb, etc...no problem.

However, the client is posting without specifying the content-type, so the default is application/x-www-form-urlencoded and so when I use the following code var thisVar = this.request.body.myKey the value thisVar is undefined so I cannot easily parse the body of the http request.

Make sense?


Solution

  • Yes, I am answering my own question but hopefully it will help someone out there.

    After a series of just printing out the http response, I found out that my raw data had the following characteristics that prevented it from being parsed into a JSON object:

    1. Backslash escapes of double-quotes
    2. The entire raw string after 'JSON.stringified' was a key with surrounded double-quotes, and then basically an empty value.

    Example: {"{ \"myKey\" : \"ValueA\"}":""}

    I used the string.replace() function to fix #1 and #2. (see sample code below)

    Example: { "myKey" : "ValueA"}

    After applying fix #1 and #2 I could then do a JSON.parse() on the JSON string.

    Here's my code with fixes #1 and #2:

    Router.route('/api/insert/test', function(){
    
    this.response.statusCode = 200;
    
    this.response.setHeader("Content-Type", "application/json");
    
    this.response.setHeader("Access-Control-Allow-Origin", "*");
    
    this.response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    
    
    
    var re = /\\/g;
    
    var re2 = /\{\"/g;
    
    var re3 = /\":\"\"\}/g;
    
    var json_str = JSON.stringify(this.request.body);
    
    var json_str_after_re = json_str.replace(re, '').replace(re2, '').replace(re3, '');
    
    var json = JSON.parse(json_str_after_re);
    
    
    
    this.response.end('this is ' + json_str_after_re + ' and value is ' + json.myKey);
    
    }, {where: 'server'});