Ken Peck

Ken Peck

  • NA
  • 1
  • 1.2k

Need a web service for NIH toolbox

Jun 28 2016 5:26 PM

My company is using an ipad app called NIH Toolbox. It collects information and when done you can upload the data to a web location via a web service. SO I created a rest web service in visual studio 2015, and it works for me but not the NIH toolbox software. The manual includes an example of a web service that can be used with their app but I can't figure out where to begin with the code they include.

**Sample webservice to be used configuration reference:**
If Export to Web is enabled on the iPad, all of the data generated by this app will be immediately sent via a web service to the URL set by the user.
Data export to web feature posts .csv files to a web location specified by the app user. Data export to web security is handled in the form of basic authentication. Web service should be set to accept basic authentication. Web service implementation should also acknowledge successful transfer of the file in the form of json object.

- Data upload webservice

  o Form action: POST

  o Form request parameters:
 ?
   upload: optional. Data are attached to this variable.

  o Form Response json object format:
 
    {

      “error”:error_code, (error code of 0 indicates successfully transfer of file).
      “message”:”message” (optional)

    }

**Sample of web service implementation in .NET (c#):**

    /***************** BASIC AUTHENTICATION */
    if (ctx.Request.HttpMethod == "OPTIONS")
    {
       ctx.Response.ContentType = "text";
       ctx.Response.ContentEncoding = Encoding.UTF8;
       ctx.Response.AppendHeader("Content-Length", "0");
       ctx.Response.AppendHeader("Connection", "keep-alive");
       ctx.Response.AppendHeader("Access-Control-Allow-Origin", "*");
       ctx.Response.AppendHeader("Access-Control-Allow-Headers", "Authorization, Origin, content-type, accept");
       ctx.Response.AppendHeader("Access-Control-Allow-Credentials", "true");
       ctx.Response.AppendHeader("Access-Control-Allow-Methods", "OPTIONS, GET, POST");
       return;
    }
    if (ctx.Request.Headers["Authorization"] == null)
    {
       ctx.Response.StatusCode = 401;
       ctx.Response.AppendHeader("WWW-Authenticate", "Basic");
       return;
    }

    //validate authentication
    if
    (!Authenticate.ValidateRequest(ctx.Request.Headers["Authorization"].ToString()))
    {
         ctx.Response.StatusCode = 401;
         return;
    }

    //save file
         if (ctx.Request.HttpMethod.ToUpper() == "POST")
    {
          try
          {
             if (ctx.Request.Files.Count > 0)
             {
                HttpPostedFile myFile = ctx.Request.Files[0];
                if (myFile != null && myFile.ContentLength > 0)
                {
                   Stream fileContents = myFile.InputStream;
                   string fileName = Path.GetFileName(myFile.FileName);
                  string path =
               Path.Combine(ctx.Server.MapPath("~/UploadedData"), fileName);
                   myFile.SaveAs(path);
                   ctx.Response.ContentType = "application/json";
                   ctx.Response.Write("{\"error\":\"0\"}");
                   return;
                }
           }
           else
           {
              ctx.Response.StatusCode = 1;
              return;
           }
      }
      catch (Exception e)
      {
          ctx.Response.StatusCode = 1;
          return;
      }
    }

I've just not spent a great deal of time working with web services like this. I get the feeling I have everything I need right here, I just don't know how to use this code to build the web service. If some wonderful person could push me in the correct direction, I would be very thankful.
 


Answers (2)