question

yexioamu avatar image
0 Likes"
yexioamu asked yexioamu commented

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

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

FlexSim 21.2.4
http
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

yexioamu avatar image yexioamu commented ·

@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 0 ·

1 Answer

·
Ben Wilson avatar image
0 Likes"
Ben Wilson answered yexioamu commented

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


1679485804486.png (111.3 KiB)
1679485943587.png (137.5 KiB)
· 9
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

yexioamu avatar image yexioamu commented ·

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 0 ·
yexioamu avatar image yexioamu commented ·

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 0 ·
Ben Wilson avatar image Ben Wilson ♦♦ yexioamu commented ·

@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 0 ·
1679575905854.png (76.8 KiB)
yexioamu avatar image yexioamu Ben Wilson ♦♦ commented ·

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 0 ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.