wavded / ogre

ogr2ogr geojson-focused web client
http://ogre.adc4gis.com
MIT License
266 stars 78 forks source link

JSON Post Request C# issue ({"error":true,"msg":"No json provided"}) #64

Closed livewirewarrior closed 3 years ago

livewirewarrior commented 7 years ago

I'm currently trying to use an HTTP POST Request to convert a GeoJSON to a shapefile but I keep getting the error message {"error":true,"msg":"No json provided"}

My current code is as followed.

namespace ConsoleApplication4 { class Program { public static async Task click1() { HttpClient client = new HttpClient(); // creates the client

        string jsonFileText = System.IO.File.ReadAllText(@"C:\Users\d14956\Desktop\map.json"); //reads from a geojson file

        HttpContent content = new System.Net.Http.StringContent("{\"json\": \"firstName\": \"John\" }", System.Text.Encoding.UTF8, "application/json");
        client.BaseAddress = new Uri("http://ogre.adc4gis.com/convertJson");
        var response = await client.PostAsync(client.BaseAddress,content);
        var fileStream = File.Create(@"C:\Users\d14956\Desktop\lol1.zip"); //creates files
        await response.Content.CopyToAsync(fileStream); // outputs content to file
        Console.WriteLine(await response.Content.ReadAsStringAsync());

        Console.ReadLine();
        return "";
    }

    public static void Main(string[] args)
    {
        string x = click1().Result;

    }
}

}

livewirewarrior commented 7 years ago

@evermanwa

livewirewarrior commented 7 years ago

@evermanwa did you ever resolve this?

evermanwa commented 7 years ago

@livewirewarrior - Check my answer for the solution I came up with.

https://github.com/wavded/ogre/issues/54

livewirewarrior commented 7 years ago

@evermanwa for prms.Add("json,json) is your json variable just a string that you read in?

evermanwa commented 7 years ago

Yes, all I had to do was read in the geojson as a string and it worked for me. Are you receiving any errors?

On Tue, Apr 11, 2017 at 3:08 PM, livewirewarrior notifications@github.com wrote:

@evermanwa https://github.com/evermanwa for prms.Add("json,json) is your json variable just a string that you read in?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/wavded/ogre/issues/64#issuecomment-293385178, or mute the thread https://github.com/notifications/unsubscribe-auth/AQvOZg71lNTvLlOs4g1Ur2AbkvJZnTupks5ru92ygaJpZM4M6iIC .

livewirewarrior commented 7 years ago

@evermanwa how did you save the data that you should have received from OGRE? All I see is that you have the return method. I assume you saved it to a file?>

evermanwa commented 7 years ago

I will show you how I saved it when I get home. I actually saved it to a stream, but you could save it to a file stream if you wanted

On Apr 11, 2017 6:16 PM, "livewirewarrior" notifications@github.com wrote:

@evermanwa https://github.com/evermanwa how did you save the data that you should have received from OGRE? All I see is that you have the return method. I assume you saved it to a file?>

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/wavded/ogre/issues/64#issuecomment-293427309, or mute the thread https://github.com/notifications/unsubscribe-auth/AQvOZp2JgkAWSUvmdXBqIRAmJUAuIicmks5rvAmzgaJpZM4M6iIC .

livewirewarrior commented 7 years ago

@evermanwa thanks It would help a lot to know how you read in the file and exported it.

evermanwa commented 7 years ago

code for getting my jsonString, changing the filenames, and I send it back to a html client as a stream You could also save it out here to a filesstream if you like. Works the same.

                    var jsonResponse = Encoding.UTF8.GetString(response);

//This replace is due to some junk that the ArcGIS json generator puts in there.
//Causes an error in converting to SHP file
                    jsonResponse = jsonResponse.Replace("0,null", "0");

                    byte[] zipFile = ExecuteShapeFileDownload(jsonResponse);

                    zipFile = ChangeFileNames(zipFile, name);

                    Stream returnStream = new MemoryStream();
                    returnStream.Write(zipFile, 0, zipFile.Length);
                    returnStream.Seek(0, SeekOrigin.Begin);

                    ret.StatusCode = HttpStatusCode.OK;
                    ret.Content = new StreamContent(returnStream);
                    ret.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/zip");
                    ret.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("inline") { FileName = name + "_" + filedt + ".zip" };
                    ret.Content.Headers.ContentLength = returnStream.Length;

This function download the shape zip file into a byte array

        private byte[] ExecuteShapeFileDownload(string json)
        {
            try
            {
                using (WebClient client = new WebClient())
                {
                    var prms = new System.Collections.Specialized.NameValueCollection();
                    prms.Add("json", json);
                    byte[] response = client.UploadValues("http://ogre.adc4gis.com/convertJson", "POST", prms);
                    return response;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return null;
        }

//This function is so we can have our own filesnames to differentiate the shape files from each other. //Default they come back as "OGRGeoJSON" as the name

        private byte[] ChangeFileNames(byte[] file, string fileName)
        {
            Stream stream = new MemoryStream(file);
            Stream returnStream = new MemoryStream();
            ZipFile tempZipFile = ZipFile.Read(stream);
            string timeStamp = DateTime.Now.ToString("MMddyy-h-m-s");
            string tempName, newFileName;
            newFileName = fileName + "-" + timeStamp;

            for (int i = 0; i < tempZipFile.Count; i++)
            {
                tempName = tempZipFile[i].FileName;
                tempZipFile[i].FileName = tempName.Replace("OGRGeoJSON", newFileName);
            }

            tempZipFile.Save(returnStream);

            return ((MemoryStream)returnStream).ToArray();
        }

This is the exact code showing how I did it. Hope this helps.

evermanwa commented 7 years ago

Were you able to get your problem resolved?

On Tue, Apr 11, 2017 at 7:41 PM, livewirewarrior notifications@github.com wrote:

Reopened #64 https://github.com/wavded/ogre/issues/64.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/wavded/ogre/issues/64#event-1039331116, or mute the thread https://github.com/notifications/unsubscribe-auth/AQvOZisj4kVTARr3n8IZKrs_jvYulYhgks5rvB3CgaJpZM4M6iIC .

github-actions[bot] commented 3 years ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] commented 3 years ago

This issue was closed because it has been stalled for 5 days with no activity.