Support

Expand all | Collapse all

REST API Create Item POST 405 Error

  • 1.  REST API Create Item POST 405 Error

    Posted 12-05-2019 12:07

    I'm trying to write a program in C# using the REST API that reads a word document, generates a list of structures, then creates new items in Jama representing these struct instances. However, running the program results in the line "HttpWebResponse response = (HttpWebResponse)request.GetResponse();" throwing an exception. The exception is: 'The remote server returned an error: (405) Method Not Allowed.' The method that interfaces with Jama is below. What could be causing the problem?

    public void uploadToJama(List<SoftReq> requirements)
    {
        string baseUrl = "https://nz-jama01.fphcare.com/rest/latest";
    
        // Username and password should be stored according
        // to your organization's security policies
        string username = "API_User";
        string password = "*****";
        int projectID = 46;
    
        ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseUrl);
        request.Method = WebRequestMethods.Http.Post;
        request.ContentType = "application/json";
    
        string credentials = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
        request.Headers["Authorization"] = "Basic " + credentials;
    
        using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            string json = "{\"project\":" + projectID + ",\"itemType\":137,\"fields\":{\"name\":\"" + requirements[0].name + "\",\"description\":\"" + requirements[0].desc +
                "\",\"trace_id\":\"" + requirements[0].trace + "\"},\"location\":{\"parent\":{\"project\":" + projectID + "}}}";
    
            streamWriter.Write(json);
        }
    
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
        Stream stream = response.GetResponseStream();
        StreamReader streamReader = new StreamReader(stream);
        string s = streamReader.ReadToEnd();
    
        Console.WriteLine(s);
        Console.ReadKey();
    }
    


    ------------------------------
    Jack
    ------------------------------


  • 2.  RE: REST API Create Item POST 405 Error

    Posted 12-05-2019 13:57
    Jack:

    Does this script work when you use our Swaggar REST API application, or through Postman?

    Best,

    ------------------------------
    Chloe Elliott
    Jama Software
    Portland OR
    ------------------------------



  • 3.  RE: REST API Create Item POST 405 Error

    Posted 12-05-2019 14:20
    Hi Chloe, thanks for the help, I'm very new to REST. I tried the following in Swagger:
    {
      "project": 46,
      "itemType": 137,
      "location": {
        "parent": {
          "project": 46
        }
      },
      "fields": {
        "name": "thing1",
        "description": "thing2",
        "trace_id": "thing3"
      }
    }

    I get a Bad Request error with the message: "Invalid document location. Software Requirement cannot be under project." I was unsure what value to put for item under parent.



    ------------------------------
    Jack
    ------------------------------



  • 4.  RE: REST API Create Item POST 405 Error

    Posted 12-05-2019 14:38
    Thanks Jack,

    Which endpoint are you using in Swaggar?

    ------------------------------
    Chloe Elliott
    Jama Software
    Portland OR
    ------------------------------



  • 5.  RE: REST API Create Item POST 405 Error

    Posted 12-05-2019 15:25
    Hi Chloe,

    I'm unfamiliar with the term 'endpoint'. I've fixed up the body in Swagger to be as follows:
    {
      "project": 46,
      "itemType": 137,
      "location": {
        "parent": {
          "item": 14403
        }
      },
      "fields": {
        "name": "thing1",
        "description": "thing2",
        "trace_id": "thing3"
      }
    }

    Now it executes without errors and successfully updates Jama as expected. I've modified the variable 'json' to be as follows:

    string json = "{ \"project\": 46, \"itemType\": 137, \"location\": { \"parent\": { \"item\": 14403 } }, \"fields\": { \"name\": \"thing1\", \"description\": \"thing2\", \"trace_id\": \"thing3\" }}";

    However, I'm still getting a 405 error.



    ------------------------------
    Jack
    ------------------------------



  • 6.  RE: REST API Create Item POST 405 Error

    Posted 12-05-2019 15:55

    Hi Jack,

    Looking at your code I can't see where you are adding the endpoint to your URL. Try changing
    string baseUrl = "https://nz-jama01.fphcare.com/rest/latest";

    to 

    string baseUrl = "https://nz-jama01.fphcare.com/rest/v1/items";
    This tells the API to use the "item" endpoint. I also changed "latest" to "v1". Currently "latest" and "v1" resolve to the same thing, but we are making an API change soon that will make a difference to this, so it's just a good practice to start using "v1".

    Everything else looks good. I hope this helps!

    ------------------------------
    Jason Ritz
    Jama Software
    OR
    ------------------------------



  • 7.  RE: REST API Create Item POST 405 Error

    Posted 12-05-2019 17:03
    Edited by Jack Qiu 12-05-2019 17:08
    Hi Jason,

    That seemed to do the trick. I've now changed the string json back to what it should be:
    string json = "{ \"project\": 46, \"itemType\": 137, \"location\": { \"parent\": { \"item\": 14403 } }, \"fields\": { \"name\": \"" + requirements[0].name + "\", \"description\": \"" + requirements[0].desc + "\", \"trace_id\": \"" + requirements[0].trace + "\" }}";

    But now I'm getting a 400 error. Some of the fields have new line characters which have a backslash. Replacing all the newline characters with spaces avoids this error. Is there any workaround without getting rid of the newlines?



    ------------------------------
    Jack
    ------------------------------



  • 8.  RE: REST API Create Item POST 405 Error

    Posted 12-06-2019 10:20
    Hi Jack,

    Glad to hear that change worked for you. As for the newline characters, these will break the json format so they do need to be stripped. You can add a line like this to your code to strip the \n's and \r's that are in your string:

    string json = Regex.Replace(s, @"\t|\r", "");

    Hope that helps!
    Jason




    ------------------------------
    Jason Ritz
    Jama Software
    Portland OR
    ------------------------------