Http request using "POST" method can't pass parameter

Http request using "POST" method can't pass parameter

yexioamu
Not applicable
457 Views
11 Replies
Message 1 of 12

Http request using "POST" method can't pass parameter

yexioamu
Not applicable

[ FlexSim 21.2.4 ]

Hi, I have a question about Http Request. When I want to request data from a remote server, the interface of the server only can be requested by "POST" method. I used two functions that I can find in this forum website.

One is applicationcommand("sendhttprequest", verb, server, object, data, silent, result), the value of verb is "POST", server is the IP of the remote server, object is the path of the interface, when I run the model, I can see the call record of the interface in the remote server, but it reports "The passed parameters is empty", It seems like the data is not passed.

string verb = "POST";
//this is the name or IP address of the server the request will be sent to
string server =  "xx.xxx.xxx.xxx"; //it is the IP of the remote server
//object represents the rest of the URL after the server name
string data ="?&reqCode=123";
string object =  "/services/genAgvTask";
//silent is a flag indicating whether you want any errors to be printed to FlexSim's system console (Debug > System Console)
int silent = 1;
//specify a text node in the tree where the server's response will be written
treenode result = node("/Tools/result", model());
//send the HTTP request
applicationcommand("sendhttprequest", verb, server, object, data, silent, result

Another is Http.Request, my code like this,

Http.Request request = Http.Request("IP/services/genAgvTask");
request.method = Http.Method.Post;
request.data = "reqCode=123";
request.useSSL = 0;
Http.Response response = request.sendAndWait();
string json = response.value;
Map map = JSON.parse(json

But it still can't work, the remote server still reports "The passed parameters is empty".

And then, I searched the docs(Post Method), it show that the description of the "Post" method is "Method to create or replace a target resource with specified data", can this method request data from server?

I'm so confused about this question a few weeks, can you give me some suggestions, and offer a example about request data by using "POST" method.

Thank you very much!

Yexioamu

0 Likes
Accepted solutions (1)
458 Views
11 Replies
Replies (11)
Message 2 of 12

yexioamu
Not applicable

@Ben Wilson@Phil BoBo Dear Mr. Wilson and Mr.BoBo, I have read several questions that answered from you, But most of them is the "GET" method, so i still can't got the correct use method. Can you pay attention to this issue when you are free?Thank you very much!

0 Likes
Message 3 of 12

Ben_WilsonADSK
Community Manager
Community Manager
Accepted solution

Both methods look like they work fine. I just used httpbin.org, which mirrors back to you whatever you send it. I tested against their POST endpoint at httpbin.org/post.

Here are the results using Http.Request

1679485804486.png

And using application command sendhttprequest

1679485943587.png

My sample model file is attached.

http_post_test_v21.2.fsm

0 Likes
Message 4 of 12

yexioamu
Not applicable

I tried your sample model, it work well, but my model is still not work, Maybe it is the problem of the remote server interface,I will check it with my R&D colleagues,Mr. Wilson, I owe you my best thanks! I will reply you the follow-up results here in the next few days.

0 Likes
Message 5 of 12

yexioamu
Not applicable

Hi, Mr.Wilson, i check it with my colleagues, Maybe the problem is my remote server requires header is 'application/json', but the header of this two method in flexsim(version 21.2.4) is 'application/x-www-form-urlencoded', I read your comments on this question(Sending Http.request for json content type), but i can't understand the custom labelProperties property ,can it solve my problem? or any other solution?

0 Likes
Message 6 of 12

Ben_WilsonADSK
Community Manager
Community Manager

@yexioamu, FlexSim version 22.1.0 added the ability to set custom request headers for Http.Request (documentation, release notes).

In the attached example you can see the request.headers setter, where I specify JSON, and in the response that header is mirrored back.

1679575905854.png

You would need to upgrade your FlexSim version to version 22.1 or higher take advantage of this new feature.

http_post_test_v22.1.fsm

0 Likes
Message 7 of 12

yexioamu
Not applicable

Thank for your answer, i tried the request.headers you suggested in the version 23.05 by using PF, it works well! But due to my company software purchase policy, i can't upgrade my FlexSim version quickly, does it possible that achieve it in version 21.2.4, or can i achieve it by using DLL function? If it is possible, can i get some references from anywhere?

0 Likes
Message 8 of 12

philboboADSK
Autodesk
Autodesk

Yes, if you want to use C++, you can use the 21.2 Module SDK or the FlexSim DLL Maker (in the 'Modules' section of https://account.flexsim.com/downloads/).

For an example of sending HTTP requests, see: https://learn.microsoft.com/en-us/windows/win32/winhttp/winhttp-sessions-overview



Phil BoBo
Sr. Manager, Software Development
Message 9 of 12

yexioamu
Not applicable

Thanks for your answer! i will try it. thanks again!

0 Likes
Message 10 of 12

yexioamu
Not applicable

Hi, BoBo, i am tring the dll maker, i use the winhttp method you suggested in a pure cpp project at first, and it work well.

Then, i create a function named HttpRequest in the mydll.cpp of the dllmaker, and then use const char* variables to put the parameters from flexsim user command, but it seems like the order of the parstr is disordered, and some parameters is repeated, i am confused about it.

the-screen-shot-of-first-dll.png

and then, i use string to put the parameters from flexsim and transform to wchar_t* so that the winhttp method can accept, but it just display the first letter of the value, and iti is also work well in the pure cpp project. i am confused about it too.

the-screen-shot-of-second-dll.png

My code in flexsim is next, command2 is a user command.

string server =  "xx.xx.xx.xx";

string verb = "POST";

string path =  "/services/genAgvTask";

string headers = "Content-Type: application/json";

string data ="{\"reqCode\":\"123\"}";

command2(server, verb, path, headers, data);

Could you help me? Thank you!

attachment.zip

0 Likes
Message 11 of 12

philboboADSK
Autodesk
Autodesk

The FlexSim Support answer to your question is to simply use the latest FlexSim version. We've already solved this issue.

If you want detailed help in resolving this in an older version, contact your local distributor for direct support. @CSN



Phil BoBo
Sr. Manager, Software Development
0 Likes
Message 12 of 12

yexioamu
Not applicable
What a pity for me,whatever,I am appreciated for your answers,thanks again
0 Likes