jperelli / osm-static-maps

Openstreetmap static maps is a nodejs lib, CLI and server open source inspired on google static map service
http://osm-static-maps.herokuapp.com/
GNU General Public License v2.0
160 stars 52 forks source link

Fix post request method #13

Closed mateusjunges closed 4 years ago

mateusjunges commented 4 years ago

While using this library, i have tried to send a POST request with this JSON object:

    {   
  "geojson": [  
    {   
      "type": "Feature",    
      "properties": {   
        "party": "Republican"   
      },    
      "geometry": { 
        "type": "Polygon",  
        "coordinates": [    
          [ 
            [   
              -104.05,  
              48.99 
            ],  
            [   
              -97.22,   
              48.98 
            ],  
            [   
              -96.58,   
              45.94 
            ],  
            [   
              -104.03,  
              45.94 
            ],  
            [   
              -104.05,  
              48.99 
            ]   
          ] 
        ]   
      } 
    },  
    {   
      "type": "Feature",    
      "properties": {   
        "party": "Democrat" 
      },    
      "geometry": { 
        "type": "Polygon",  
        "coordinates": [    
          [ 
            [   
              -109.05,  
              41.00 
            ],  
            [   
              -102.06,  
              40.99 
            ],  
            [   
              -102.03,  
              36.99 
            ],  
            [   
              -109.04,  
              36.99 
            ],  
            [   
              -109.05,  
              41.00 
            ]   
          ] 
        ]   
      } 
    }   
  ],    
  "tileserverUrl": "http://ysera.openstreetmap.org/{z}/{x}/{y}.png",    
  "imagemin": "true"    
}

It sent back to me the following error:

Error: Syntax Error: Unexpected identifier

While debugging, i figured out that the geojson object received via GET (which works fine) and via POST was different.

This is the gejson received via GET method:

[
   {
      "type":"Feature",
      "properties":{
         "party":"Republican"
      },
      "geometry":{
         "type":"Polygon",
         "coordinates":[
            [
               [
                  -104.05,
                  48.99
               ],
               [
                  -97.22,
                  48.98
               ],
               [
                  -96.58,
                  45.94
               ],
               [
                  -104.03,
                  45.94
               ],
               [
                  -104.05,
                  48.99
               ]
            ]
         ]
      }
   },
   {
      "type":"Feature",
      "properties":{
         "party":"Democrat"
      },
      "geometry":{
         "type":"Polygon",
         "coordinates":[
            [
               [
                  -109.05,
                  41.00
               ],
               [
                  -102.06,
                  40.99
               ],
               [
                  -102.03,
                  36.99
               ],
               [
                  -109.04,
                  36.99
               ],
               [
                  -109.05,
                  41.00
               ]
             ] 
         ]
      }
   }
]

And this is the geojson received via POST:

[
   {
      "type":"Feature",
      "properties":{
         "party":"Republican"
      },
      "geometry":{
         "type":"Polygon",
         "coordinates":[
            "Array"
         ]
      }
   },
   {
      "type":"Feature",
      "properties":{
         "party":"Democrat"
      },
      "geometry":{
         "type":"Polygon",
         "coordinates":[
            "Array"
         ]
      }
   }
]

I fixed it by adding a JSON.stringfy(options.geojson) if the request was sent via POST method. This PR does not introduce breaking changes.

Thanks!

jperelli commented 4 years ago

Thanks a lot for the pull request, I ended up fixing it in another way, that was the same I applied to other object arguments

jperelli commented 4 years ago

@mateusjunges also published fix in v3.5.1

mateusjunges commented 4 years ago

@jperelli Thanks!