Problem with online hosted json file and to Post them per c# HttpClient

Viewed 75

Im programming an C# Android App and I store my data on the website jsonbin.io I already used this website already in a javascript project, it worked fine there. Reading of the data is already implemented and working, but not the function to post. On the website you have to put a so-called X-Master-Key in the header for verification.

JSONBIN Link

The working code of the HttpRequest:

           WebRequest web = HttpWebRequest.Create("https://api.jsonbin.io/b/"BIN-CODE"/latest");
        web.Headers.Add("X-Master-Key:THE-MASTER-KEY");
        web.GetResponse();
        HttpWebResponse response = web.GetResponse() as HttpWebResponse;
        using (Stream responseStream = response.GetResponseStream())
        {
            StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
            json = reader.ReadToEnd()
        }

The working code with the reading and writing part from my JavaScript Project:

 writeJSON: function()
{
    var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
    let req = new XMLHttpRequest();        
    req.open("PUT", "https://api.jsonbin.io/b/"BIN-CODE"", true);
    req.setRequestHeader("Content-Type", "application/json");
    req.setRequestHeader("X-Master-Key", "THE-MASTER-KEY");
    
    req.send(JSON.stringify(this.file));
},
readJSON: async function () {
    var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
   
    let req = new XMLHttpRequest();
    
    req.onreadystatechange = () => {
      if (req.readyState == 4) {
        this.file= JSON.parse(req.responseText);
      }
    
    };
    
    req.open("GET", "https://api.jsonbin.io/b/"BIN-CODE"/latest", true);
    req.setRequestHeader("X-Master-Key", "THE-MASTER-KEY");
    req.send();
  
}

Here are also some pictures of the website for a better understanding:

editting the JSON on the Page

call up the URL which is also addressed in the code

0 Answers
Related