Proplem with Post in REST-API (C#)

H.-J. Meyer
H.-J. Meyer Member Posts: 3

Hi together.

I am facing the problem, that all queries work as expected, but post doesn't.
My goal is to post test groups, but because it didn't work, I have tried to post an item first. What I get is an error 400!

      public void uploadToJama(int projectID)
        {
            string baseUrl = MainForm.JAMA_BASE_URL + DEFAULT_API_VERSION + "/items";
            string json;

            //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(MainForm.UserName + ":" + MainForm.Password));
            request.Headers["Authorization"] = "Basic " + credentials;

            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                // json = "{"testplans":8545796,"testgroup"{"name":"one more}}";
                // json = "{"project":" + projectID.ToString() + ", "itemType":44, "location":{"parent":{"item":8382393}}, "fields":{"name":"TestItem","description":"Test-Content"}}";
                json = "{ "project": 171, "itemType": 44, "location": { "parent": { "item": 8382393 } }, "fields": { "name": "TestItem", "description": "Test-Content" }}";

                streamWriter.Write(json);
                json = Regex.Replace(json, @" | ", "");
            }

            HttpWebResponse response = (HttpWebResponse) request.GetResponse();

            Stream stream = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(stream);
            string s = streamReader.ReadToEnd();

            Console.WriteLine(s);
            Console.ReadKey();
        }

Does anybody have an idea what went wrong?

Thanks in advance H.-J.

Sorry for the typo in the title, but I couldn't fix it later.

Comments

  • Dimitrios Pananakis
    Dimitrios Pananakis Member, Jama Validation Kit (JVK) + Functional Safety Kit (FSK) Posts: 57
    edited July 2023

    Hi,
    I am not a C# expert but I noticed that there are spaces between the colon of the JSON keys and their values which probably is invalid syntax.  I would also try to use the exact string you are passing to the request with the Swagger UI and see if that is successful. That may help you identify the cause.
    Warm regards

    Dimitrios

    -------------------------------------------
    Original Message:
    Sent: 06-22-2023 00:04
    From: H.-J. Meyer
    Subject: Proplem with Post in REST-API (C#)

    Hi together.

    I am facing the problem, that all queries work as expected, but post doesn't.
    My goal is to post test groups, but because it didn't work, I have tried to post an item first. What I get is an error 400!

          public void uploadToJama(int projectID)
            {
                string baseUrl = MainForm.JAMA_BASE_URL + DEFAULT_API_VERSION + "/items";
                string json;

                //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(MainForm.UserName + ":" + MainForm.Password));
                request.Headers["Authorization"] = "Basic " + credentials;

                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    // json = "{"testplans":8545796,"testgroup"{"name":"one more}}";
                    // json = "{"project":" + projectID.ToString() + ", "itemType":44, "location":{"parent":{"item":8382393}}, "fields":{"name":"TestItem","description":"Test-Content"}}";
                    json = "{ "project": 171, "itemType": 44, "location": { "parent": { "item": 8382393 } }, "fields": { "name": "TestItem", "description": "Test-Content" }}";

                    streamWriter.Write(json);
                    json = Regex.Replace(json, @" | ", "");
                }

                HttpWebResponse response = (HttpWebResponse) request.GetResponse();

                Stream stream = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(stream);
                string s = streamReader.ReadToEnd();

                Console.WriteLine(s);
                Console.ReadKey();
            }

    Does anybody have an idea what went wrong?

    Thanks in advance H.-J.

    Sorry for the typo in the title, but I couldn't fix it later.

  • H.-J. Meyer
    H.-J. Meyer Member Posts: 3
    edited July 2023

    Dear Dimitrios
    Thank you for the suggestion, but removing the spaces didn't solve the problem. I still get the error 400.

    Best regards
    H-J.

    ------------------------------
    H.-J. Meyer
    Panasonic Industrial Devices Europe GmbH
    Lüneburg
    ------------------------------
    -------------------------------------------
    Original Message:
    Sent: 07-13-2023 02:56
    From: Dimitrios Pananakis
    Subject: Proplem with Post in REST-API (C#)

    Hi,
    I am not a C# expert but I noticed that there are spaces between the colon of the JSON keys and their values which probably is invalid syntax.  I would also try to use the exact string you are passing to the request with the Swagger UI and see if that is successful. That may help you identify the cause.
    Warm regards

    Dimitrios

    Original Message:
    Sent: 06-22-2023 00:04
    From: H.-J. Meyer
    Subject: Proplem with Post in REST-API (C#)

    Hi together.

    I am facing the problem, that all queries work as expected, but post doesn't.
    My goal is to post test groups, but because it didn't work, I have tried to post an item first. What I get is an error 400!

          public void uploadToJama(int projectID)
            {
                string baseUrl = MainForm.JAMA_BASE_URL + DEFAULT_API_VERSION + "/items";
                string json;

                //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(MainForm.UserName + ":" + MainForm.Password));
                request.Headers["Authorization"] = "Basic " + credentials;

                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    // json = "{"testplans":8545796,"testgroup"{"name":"one more}}";
                    // json = "{"project":" + projectID.ToString() + ", "itemType":44, "location":{"parent":{"item":8382393}}, "fields":{"name":"TestItem","description":"Test-Content"}}";
                    json = "{ "project": 171, "itemType": 44, "location": { "parent": { "item": 8382393 } }, "fields": { "name": "TestItem", "description": "Test-Content" }}";

                    streamWriter.Write(json);
                    json = Regex.Replace(json, @" | ", "");
                }

                HttpWebResponse response = (HttpWebResponse) request.GetResponse();

                Stream stream = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(stream);
                string s = streamReader.ReadToEnd();

                Console.WriteLine(s);
                Console.ReadKey();
            }

    Does anybody have an idea what went wrong?

    Thanks in advance H.-J.

    Sorry for the typo in the title, but I couldn't fix it later.

  • Dimitrios Pananakis
    Dimitrios Pananakis Member, Jama Validation Kit (JVK) + Functional Safety Kit (FSK) Posts: 57
    edited July 2023

    I would try first the Swagger UI to perform the exact request you are trying. Doing it this way would allow you to see if the actual POST parameters (json contents) are the culprit or the C# implementation.

    If the request is not ok in Swagger, try another parent item to do a POST request. 
    For the C# code, its not a good practice to construct json the way you do. I think you should use a serializer library instead. It will make your code more readable, maintainable and robust. There are many libraries available for C#, such as Newtonsoft.Json, System.Text.Json, DataContractJsonSerializer, etc.

    Warm regards

    Dimitrios

    -------------------------------------------
    Original Message:
    Sent: 07-13-2023 03:34
    From: H.-J. Meyer
    Subject: Proplem with Post in REST-API (C#)

    Dear Dimitrios
    Thank you for the suggestion, but removing the spaces didn't solve the problem. I still get the error 400.

    Best regards
    H-J.

    ------------------------------
    H.-J. Meyer
    Panasonic Industrial Devices Europe GmbH
    Lüneburg
    ------------------------------

    Original Message:
    Sent: 07-13-2023 02:56
    From: Dimitrios Pananakis
    Subject: Proplem with Post in REST-API (C#)

    Hi,
    I am not a C# expert but I noticed that there are spaces between the colon of the JSON keys and their values which probably is invalid syntax.  I would also try to use the exact string you are passing to the request with the Swagger UI and see if that is successful. That may help you identify the cause.
    Warm regards

    Dimitrios

    Original Message:
    Sent: 06-22-2023 00:04
    From: H.-J. Meyer
    Subject: Proplem with Post in REST-API (C#)

    Hi together.

    I am facing the problem, that all queries work as expected, but post doesn't.
    My goal is to post test groups, but because it didn't work, I have tried to post an item first. What I get is an error 400!

          public void uploadToJama(int projectID)
            {
                string baseUrl = MainForm.JAMA_BASE_URL + DEFAULT_API_VERSION + "/items";
                string json;

                //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(MainForm.UserName + ":" + MainForm.Password));
                request.Headers["Authorization"] = "Basic " + credentials;

                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    // json = "{"testplans":8545796,"testgroup"{"name":"one more}}";
                    // json = "{"project":" + projectID.ToString() + ", "itemType":44, "location":{"parent":{"item":8382393}}, "fields":{"name":"TestItem","description":"Test-Content"}}";
                    json = "{ "project": 171, "itemType": 44, "location": { "parent": { "item": 8382393 } }, "fields": { "name": "TestItem", "description": "Test-Content" }}";

                    streamWriter.Write(json);
                    json = Regex.Replace(json, @" | ", "");
                }

                HttpWebResponse response = (HttpWebResponse) request.GetResponse();

                Stream stream = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(stream);
                string s = streamReader.ReadToEnd();

                Console.WriteLine(s);
                Console.ReadKey();
            }

    Does anybody have an idea what went wrong?

    Thanks in advance H.-J.

    Sorry for the typo in the title, but I couldn't fix it later.

  • H.-J. Meyer
    H.-J. Meyer Member Posts: 3
    edited July 2023

    Hi Dimitrios
    I have checked it in Swagger before, and it worked. All other requests are doing fine, also in C#, but not the post.
    Anyhow, what I have posted is just an example to find out what went wrong. It is usually serialized via JsonSerializer (System.Text.Json).

    Thank you
    H-J.

    ------------------------------
    H.-J. Meyer
    Panasonic Industrial Devices Europe GmbH
    Lüneburg
    ------------------------------
    -------------------------------------------
    Original Message:
    Sent: 07-13-2023 05:41
    From: Dimitrios Pananakis
    Subject: Proplem with Post in REST-API (C#)

    I would try first the Swagger UI to perform the exact request you are trying. Doing it this way would allow you to see if the actual POST parameters (json contents) are the culprit or the C# implementation.

    If the request is not ok in Swagger, try another parent item to do a POST request. 
    For the C# code, its not a good practice to construct json the way you do. I think you should use a serializer library instead. It will make your code more readable, maintainable and robust. There are many libraries available for C#, such as Newtonsoft.Json, System.Text.Json, DataContractJsonSerializer, etc.

    Warm regards

    Dimitrios

    Original Message:
    Sent: 07-13-2023 03:34
    From: H.-J. Meyer
    Subject: Proplem with Post in REST-API (C#)

    Dear Dimitrios
    Thank you for the suggestion, but removing the spaces didn't solve the problem. I still get the error 400.

    Best regards
    H-J.

    ------------------------------
    H.-J. Meyer
    Panasonic Industrial Devices Europe GmbH
    Lüneburg

    Original Message:
    Sent: 07-13-2023 02:56
    From: Dimitrios Pananakis
    Subject: Proplem with Post in REST-API (C#)

    Hi,
    I am not a C# expert but I noticed that there are spaces between the colon of the JSON keys and their values which probably is invalid syntax.  I would also try to use the exact string you are passing to the request with the Swagger UI and see if that is successful. That may help you identify the cause.
    Warm regards

    Dimitrios

    Original Message:
    Sent: 06-22-2023 00:04
    From: H.-J. Meyer
    Subject: Proplem with Post in REST-API (C#)

    Hi together.

    I am facing the problem, that all queries work as expected, but post doesn't.
    My goal is to post test groups, but because it didn't work, I have tried to post an item first. What I get is an error 400!

          public void uploadToJama(int projectID)
            {
                string baseUrl = MainForm.JAMA_BASE_URL + DEFAULT_API_VERSION + "/items";
                string json;

                //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(MainForm.UserName + ":" + MainForm.Password));
                request.Headers["Authorization"] = "Basic " + credentials;

                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    // json = "{"testplans":8545796,"testgroup"{"name":"one more}}";
                    // json = "{"project":" + projectID.ToString() + ", "itemType":44, "location":{"parent":{"item":8382393}}, "fields":{"name":"TestItem","description":"Test-Content"}}";
                    json = "{ "project": 171, "itemType": 44, "location": { "parent": { "item": 8382393 } }, "fields": { "name": "TestItem", "description": "Test-Content" }}";

                    streamWriter.Write(json);
                    json = Regex.Replace(json, @" | ", "");
                }

                HttpWebResponse response = (HttpWebResponse) request.GetResponse();

                Stream stream = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(stream);
                string s = streamReader.ReadToEnd();

                Console.WriteLine(s);
                Console.ReadKey();
            }

    Does anybody have an idea what went wrong?

    Thanks in advance H.-J.

    Sorry for the typo in the title, but I couldn't fix it later.

  • [Deleted User]
    [Deleted User] Posts: 152
    edited July 2023

    Thanks for posting this, H.-J., and for weighing in here, Dimitrios!

    At this point, I think the best bet here would be for H.-J. to connect with our Support team to dive deeper into this. Please file a ticket HERE, and include a link to this thread as well as any other pertinent info (such as your instance type and version number) in the body of the ticket, to move forward. 

    Thanks!

    ------------------------------
    Carly Rossi // she/her/hers
    Community Program Manager // Jama Software
    Portland, OR
    ------------------------------
    -------------------------------------------
    Original Message:
    Sent: 07-13-2023 05:59
    From: H.-J. Meyer
    Subject: Proplem with Post in REST-API (C#)

    Hi Dimitrios
    I have checked it in Swagger before, and it worked. All other requests are doing fine, also in C#, but not the post.
    Anyhow, what I have posted is just an example to find out what went wrong. It is usually serialized via JsonSerializer (System.Text.Json).

    Thank you
    H-J.

    ------------------------------
    H.-J. Meyer
    Panasonic Industrial Devices Europe GmbH
    Lüneburg
    ------------------------------

    Original Message:
    Sent: 07-13-2023 05:41
    From: Dimitrios Pananakis
    Subject: Proplem with Post in REST-API (C#)

    I would try first the Swagger UI to perform the exact request you are trying. Doing it this way would allow you to see if the actual POST parameters (json contents) are the culprit or the C# implementation.

    If the request is not ok in Swagger, try another parent item to do a POST request. 
    For the C# code, its not a good practice to construct json the way you do. I think you should use a serializer library instead. It will make your code more readable, maintainable and robust. There are many libraries available for C#, such as Newtonsoft.Json, System.Text.Json, DataContractJsonSerializer, etc.

    Warm regards

    Dimitrios

    Original Message:
    Sent: 07-13-2023 03:34
    From: H.-J. Meyer
    Subject: Proplem with Post in REST-API (C#)

    Dear Dimitrios
    Thank you for the suggestion, but removing the spaces didn't solve the problem. I still get the error 400.

    Best regards
    H-J.

    ------------------------------
    H.-J. Meyer
    Panasonic Industrial Devices Europe GmbH
    Lüneburg

    Original Message:
    Sent: 07-13-2023 02:56
    From: Dimitrios Pananakis
    Subject: Proplem with Post in REST-API (C#)

    Hi,
    I am not a C# expert but I noticed that there are spaces between the colon of the JSON keys and their values which probably is invalid syntax.  I would also try to use the exact string you are passing to the request with the Swagger UI and see if that is successful. That may help you identify the cause.
    Warm regards

    Dimitrios

    Original Message:
    Sent: 06-22-2023 00:04
    From: H.-J. Meyer
    Subject: Proplem with Post in REST-API (C#)

    Hi together.

    I am facing the problem, that all queries work as expected, but post doesn't.
    My goal is to post test groups, but because it didn't work, I have tried to post an item first. What I get is an error 400!

          public void uploadToJama(int projectID)
            {
                string baseUrl = MainForm.JAMA_BASE_URL + DEFAULT_API_VERSION + "/items";
                string json;

                //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(MainForm.UserName + ":" + MainForm.Password));
                request.Headers["Authorization"] = "Basic " + credentials;

                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    // json = "{"testplans":8545796,"testgroup"{"name":"one more}}";
                    // json = "{"project":" + projectID.ToString() + ", "itemType":44, "location":{"parent":{"item":8382393}}, "fields":{"name":"TestItem","description":"Test-Content"}}";
                    json = "{ "project": 171, "itemType": 44, "location": { "parent": { "item": 8382393 } }, "fields": { "name": "TestItem", "description": "Test-Content" }}";

                    streamWriter.Write(json);
                    json = Regex.Replace(json, @" | ", "");
                }

                HttpWebResponse response = (HttpWebResponse) request.GetResponse();

                Stream stream = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(stream);
                string s = streamReader.ReadToEnd();

                Console.WriteLine(s);
                Console.ReadKey();
            }

    Does anybody have an idea what went wrong?

    Thanks in advance H.-J.

    Sorry for the typo in the title, but I couldn't fix it later.