Ricky90
(Ricky Horne)
December 23, 2022, 8:20am
1
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.
josecgomez
(Jose C Gomez)
December 23, 2022, 2:21pm
2
I assume you are communicating with an external source?
How are you populating MtlQueue? can you give us the rest of your BPM?
klincecum
(Kevin Lincecum)
December 23, 2022, 2:22pm
3
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
klincecum
(Kevin Lincecum)
December 23, 2022, 2:40pm
4
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
}
};
Ricky90
(Ricky Horne)
December 23, 2022, 2:51pm
5
HI, the entire bpm is here.
I have added the suggestion from @klincecum but i have the error as shown below.
josecgomez
(Jose C Gomez)
December 23, 2022, 2:57pm
6
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
}
}
};
Ricky90
(Ricky Horne)
December 23, 2022, 3:07pm
7
Thank you.
I’m still receiving the same response back.
Its definitely the body that is the issue, as the original code with the static data works fine eventhough it looks a mess.
josecgomez
(Jose C Gomez)
December 23, 2022, 3:08pm
8
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
}
}
};
Ricky90
(Ricky Horne)
December 23, 2022, 3:12pm
9
Hi, Still the same error.
The bpm is definitely running as it is posting the .txt file at the end.
josecgomez
(Jose C Gomez)
December 23, 2022, 3:13pm
10
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?
Ricky90
(Ricky Horne)
December 23, 2022, 3:14pm
11
error persists. i tried that just before your last reply.
josecgomez
(Jose C Gomez)
December 23, 2022, 3:18pm
12
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);
Ricky90
(Ricky Horne)
December 23, 2022, 3:21pm
13
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)
josecgomez
(Jose C Gomez)
December 23, 2022, 3:26pm
14
Change let to var we need to serialize both so we can compare it.
Ricky90
(Ricky Horne)
December 23, 2022, 3:28pm
15
no problem.
89858.txt (215 Bytes)
I cannot see a difference.
josecgomez
(Jose C Gomez)
December 23, 2022, 3:39pm
16
Okay then your issue must be in your headers… or something else the payload is the same.
Ricky90
(Ricky Horne)
December 23, 2022, 3:48pm
17
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)
josecgomez
(Jose C Gomez)
December 23, 2022, 4:05pm
18
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.
josecgomez
(Jose C Gomez)
December 23, 2022, 4:12pm
19
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"
}
}
};