Posting to API failure

I am trying to post to an API which works with the following body

var body = 
@"{
" + "\n" +
@"    ""IMP_ORDINI"": [
" + "\n" +
@"        {
" + "\n" +
@"                ""ORD_OPERAZIONE"": ""D"",
" + "\n" +
@"                ""ORD_ORDINE"": ""RICKY_TEST03"",
" + "\n" +
@"                ""ORD_DES"": ""TEST DESCRIPTION"",
" + "\n" +
@"                ""ORD_TIPOOP"": ""P""
" + "\n" +
@"                
" + "\n" +
@"        }        
" + "\n" +
@"    ],
" + "\n" +
@"    ""IMP_ORDINI_RIGHE"": [
" + "\n" +
@"        {
" + "\n" +
@"            ""RIG_ORDINE"": ""RICKY_TEST03"",
" + "\n" +
@"            ""RIG_ARTICOLO"": ""05761"",
" + "\n" +
@"            ""RIG_QTAR"": ""100.00"",
" + "\n" +
@"            ""RIG_LOCAZIONE"" : ""1001""
" + "\n" +
@"        }
" + "\n" +
@"    ]
" + "\n" +
@"}";

But no matter what syntax i use to try and add database fields to the code the API fails.
My current body within a bpm is 

   var body = new {
        IMP_ORDINI = new {
                ORD_OPERAZIONE = "I",
                ORD_ORDINE = MtlQueue.OrderNum,
                ORD_DES = MtlQueue.OrderNum,
                ORD_TIPOOP = "P"
                },
                
              IMP_ORDINI_RIGHE = new {
                          RIG_ORDINE = MtlQueue.OrderNum,
                          RIG_ARTICOLO = MtlQueue.PartNum,
                          RIG_QTAR = MtlQueue.Quantity,
                          RIG_LOCAZIONE = MtlQueue.FromBinNum
                          }
                         };

I have several API programs running already but this seemingly simple program (compared to my others) continues to fail.

I assume you are communicating with an external source?

How are you populating MtlQueue? can you give us the rest of your BPM?

Not trying to be rude, but that top part which I am assuming is your test code has a whole lot of extra things it doesn’t need to display some json as a a string. It might be best to look into how to properly create and annotate json in your code.

What exactly is failing ? Do you have any further info ?
I’m not sure there is enough information in your post to tell you anything meaningful.

1 Like

I’m assuming this is what you meant after cleaning it up

var body = @"
{
    ""IMP_ORDINI"": [
        {
            ""ORD_OPERAZIONE"": ""D"",
            ""ORD_ORDINE"": ""RICKY_TEST03"",
            ""ORD_DES"": ""TEST DESCRIPTION"",
            ""ORD_TIPOOP"": ""P""
        }
    ],
    ""IMP_ORDINI_RIGHE"": [
        {
            ""RIG_ORDINE"": ""RICKY_TEST03"",
            ""RIG_ARTICOLO"": ""05761"",
            ""RIG_QTAR"": ""100.00"",
            ""RIG_LOCAZIONE"" : ""1001""
        }
    ]
}";



var body = new {
    IMP_ORDINI = new {
        ORD_OPERAZIONE = "I",
        ORD_ORDINE = MtlQueue.OrderNum,
        ORD_DES = MtlQueue.OrderNum,
        ORD_TIPOOP = "P"
    },
    IMP_ORDINI_RIGHE = new {
        RIG_ORDINE = MtlQueue.OrderNum,
        RIG_ARTICOLO = MtlQueue.PartNum,
        RIG_QTAR = MtlQueue.Quantity,
        RIG_LOCAZIONE = MtlQueue.FromBinNum
    }
};

The problem is in the bottom, your inner objects are not in arrays, the working example is.

Can’t remember if this is the proper syntax, but this is what you should be doing:

var body = new {
    IMP_ORDINI = new array {
        ORD_OPERAZIONE = "I",
        ORD_ORDINE = MtlQueue.OrderNum,
        ORD_DES = MtlQueue.OrderNum,
        ORD_TIPOOP = "P"
    },
    IMP_ORDINI_RIGHE = new array {
        RIG_ORDINE = MtlQueue.OrderNum,
        RIG_ARTICOLO = MtlQueue.PartNum,
        RIG_QTAR = MtlQueue.Quantity,
        RIG_LOCAZIONE = MtlQueue.FromBinNum
    }
};

HI, the entire bpm is here.

I have added the suggestion from @klincecum but i have the error as shown below.

array keyword (lowercase) isn’t a thing in C#

But I don’t know that’s your problem you aren’t putting any content inside your body is just an empty object…

You need something like

var body = new {
  IMP_ORDINI = new [] {
      new {
          ORD_OPERAZIONE = "I",
          ORD_ORDINE = MtlQueue.OrderNum,
          ORD_DES = MtlQueue.OrderNum,
          ORD_TIPOOP = "P"
      }
    },
    IMP_ORDINI_RIGHE = new [] {
      new {
          RIG_ORDINE = MtlQueue.OrderNum,
          RIG_ARTICOLO = MtlQueue.PartNum,
          RIG_QTAR = MtlQueue.Quantity,
          RIG_LOCAZIONE = MtlQueue.FromBinNum
      }
    }
};

Thank you.
I’m still receiving the same response back.

image

Its definitely the body that is the issue, as the original code with the static data works fine eventhough it looks a mess.

Looks like from your first screenshot they are all strings, add ToString() to anything that uses MtlQueue and its a number.

var body = new {
  IMP_ORDINI = new [] {
      new {
          ORD_OPERAZIONE = "I",
          ORD_ORDINE = MtlQueue.OrderNum.ToString(),
          ORD_DES = MtlQueue.OrderNum.ToString(),
          ORD_TIPOOP = "P"
      }
    },
    IMP_ORDINI_RIGHE = new [] {
      new {
          RIG_ORDINE = MtlQueue.OrderNum.ToString(),
          RIG_ARTICOLO = MtlQueue.PartNum,
          RIG_QTAR = MtlQueue.Quantity.ToString(),
          RIG_LOCAZIONE = MtlQueue.FromBinNum
      }
    }
};

Hi, Still the same error.
The bpm is definitely running as it is posting the .txt file at the end.

Can you hard code the result in the body like you did the first time (but instead of a string use the dynamic object) basically change anything that says MtlQueue to a hard coded string
Does the error persist?

error persists. i tried that just before your last reply.

Can you write your Original String to a File and then using the Dynamic Object write that JSON to a Text File and let’s compare them.

That is write this original body to a File

var oldBbody = 
@"{
" + "\n" +
@"    ""IMP_ORDINI"": [
" + "\n" +
@"        {
" + "\n" +
@"                ""ORD_OPERAZIONE"": ""D"",
" + "\n" +
@"                ""ORD_ORDINE"": ""RICKY_TEST03"",
" + "\n" +
@"                ""ORD_DES"": ""TEST DESCRIPTION"",
" + "\n" +
@"                ""ORD_TIPOOP"": ""P""
" + "\n" +
@"                
" + "\n" +
@"        }        
" + "\n" +
@"    ],
" + "\n" +
@"    ""IMP_ORDINI_RIGHE"": [
" + "\n" +
@"        {
" + "\n" +
@"            ""RIG_ORDINE"": ""RICKY_TEST03"",
" + "\n" +
@"            ""RIG_ARTICOLO"": ""05761"",
" + "\n" +
@"            ""RIG_QTAR"": ""100.00"",
" + "\n" +
@"            ""RIG_LOCAZIONE"" : ""1001""
" + "\n" +
@"        }
" + "\n" +
@"    ]
" + "\n" +
@"}";

File.WriteAllLines(oldBbody);

Then write this to a File


var body = new {
  IMP_ORDINI = new [] {
      new {
          ORD_OPERAZIONE = "I",
          ORD_ORDINE = MtlQueue.OrderNum.ToString(),
          ORD_DES = MtlQueue.OrderNum.ToString(),
          ORD_TIPOOP = "P"
      }
    },
    IMP_ORDINI_RIGHE = new [] {
      new {
          RIG_ORDINE = MtlQueue.OrderNum.ToString(),
          RIG_ARTICOLO = MtlQueue.PartNum,
          RIG_QTAR = MtlQueue.Quantity.ToString(),
          RIG_LOCAZIONE = MtlQueue.FromBinNum
      }
    }
};

var JsonBody = Newtonsoft.Json.JsonConvert.SerializeObject(body);

File.WriteAllLines(JsonBody);

JSON.txt (463 Bytes)

File is attached.
I cant use Let in my code due to missing references.
So an non serialised txt file is below…
89855.txt (202 Bytes)

Change let to var we need to serialize both so we can compare it.

no problem.
89858.txt (215 Bytes)

I cannot see a difference.

Okay then your issue must be in your headers… or something else the payload is the same.

im confused.
I have put the messy code back into the body and it works so the body has to be the issue.
the txt file is attached.
89866.txt (537 Bytes)

can you please make the dynamic code match the “messy” code by hard coding the values into the dynamic code.

Then Serialize the Dynamic Code and send both JSON files… as far as I can tell there is no difference.

This produces the exact same JSON as the “Messy” code if you use this, does it fail?

  var body = new {
    IMP_ORDINI = new [] {
        new {
            ORD_DES = "TEST DESCRIPTION",
            ORD_OPERAZIONE = "D",
            ORD_ORDINE = "RICKY_TEST03",
            ORD_TIPOOP = "P"
        }
      },
      IMP_ORDINI_RIGHE = new [] {
        new {
            RIG_ARTICOLO = "05761",
            RIG_LOCAZIONE = "1001",
            RIG_ORDINE = "RICKY_TEST03",
            RIG_QTAR = "100.00"
        }
      }

  };

Hi, that still fails :slightly_frowning_face:

200x80-18315753551922962509.jpg