Board index FlightGear Development Nasal

Possible to send data with HTTP requests?

Nasal is the scripting language of FlightGear.

Possible to send data with HTTP requests?

Postby ragerin » Thu Dec 07, 2017 7:22 pm

Hello!

I have sought around and I have a really hard time finding definite documentation about sending HTTP requests from Nasal.

I see that it is possible, but I am unsure whether I am only able to send GET requests? And can I somehow include custom data with the request?

The HTTP API I am seeking to integrate, uses endpoints in the format of http://domain.com/x/y/z and consumes arbitrary methods and included data in JSON format.


I hope that some of you guru's can give me some clearance in this regard. :) I must admit I got a little lost in the documentation and source code.


Cheers!
ragerin
 
Posts: 6
Joined: Sat Dec 02, 2017 11:28 am

Re: Possible to send data with HTTP requests?

Postby Hooray » Thu Dec 07, 2017 7:32 pm

The HTTP API I am seeking to integrate, uses endpoints in the format of http://domain.com/x/y/z and consumes arbitrary methods and included data in JSON format.

That is exactly what the Nasal http module is doing, too - it is for instance being used for by tiled map displays (canvas system):

http://wiki.flightgear.org/Canvas_Snipp ... e_tile_map
Image

See also Stuart's and Slawek's recent work:
http://wiki.flightgear.org/Canvas_News# ... lippy_Maps
Image

For additional Nasal http information, refer to:

http://wiki.flightgear.org/Howto:Making ... Nasal_APIs
https://sourceforge.net/p/flightgear/fl ... TP.cxx#l89
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: Possible to send data with HTTP requests?

Postby ragerin » Thu Dec 07, 2017 8:43 pm

Hooray wrote in Thu Dec 07, 2017 7:32 pm:
The HTTP API I am seeking to integrate, uses endpoints in the format of http://domain.com/x/y/z and consumes arbitrary methods and included data in JSON format.

That is exactly what the Nasal http module is doing, too - it is for instance being used for by tiled map displays (canvas system):

http://wiki.flightgear.org/Canvas_Snipp ... e_tile_map

See also Stuart's and Slawek's recent work:
http://wiki.flightgear.org/Canvas_News# ... lippy_Maps

For additional Nasal http information, refer to:

http://wiki.flightgear.org/Howto:Making ... Nasal_APIs
https://sourceforge.net/p/flightgear/fl ... TP.cxx#l89


Hello @Hooray and thanks for your response

This is some of the documentation that I have been looking at, but it still seems unclear to me how I would go about, for example, doing a POST request and include some data.

For example, how could I send a similar request, as the following curl command, with Nasal?

Code: Select all
curl -i -H "Content-Type: application/json" -X POST -d '{"some_key":"some_value"}' http://localhost:1234/some/api/endpoint



Thank you for taking the time to answer my beginner's question...! :oops:
ragerin
 
Posts: 6
Joined: Sat Dec 02, 2017 11:28 am

Re: Possible to send data with HTTP requests?

Postby Hooray » Thu Dec 07, 2017 8:51 pm

May I politely suggest to actually look at the code/pointers (links) I posted and then use the built-in Nasal console to see for yourself ?
You could also refer to the implementation of the recently added STAMEN layers, which are basically using the same http API.
So there is quite a bit of existing Nasal code that you could look at, and play with - and then post your code here to ask a more specific question if something should still be unclear.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: Possible to send data with HTTP requests?

Postby ragerin » Thu Dec 07, 2017 9:50 pm

Hooray wrote in Thu Dec 07, 2017 8:51 pm:May I politely suggest to actually look at the code/pointers (links) I posted and then use the built-in Nasal console to see for yourself ?
You could also refer to the implementation of the recently added STAMEN layers, which are basically using the same http API.
So there is quite a bit of existing Nasal code that you could look at, and play with - and then post your code here to ask a more specific question if something should still be unclear.


Hello Hooray

I understand your hesitance, but I must politely reply that I have already looked through everything that I could find, including the links you sent me.

Part of the trouble for me, is that I don't know C++, thus making it very hard for me to understand how exactly the different modules are exposed in Nasal.
The documentation on Nasal has also not been very clear to me. Perhaps it is, when I know my way around a little better.

So, using the examples from the Wiki-pages, I have been stuck with the following code:

Code: Select all
var url = "http://127.0.0.1:5000/v1/planes/";
var request = http.load(url)
    .done(func(r) print("Got response: " ~ r.status, " ", r.method, " ", r.response));


Note, that the API does not use URL encoded data exchange, but exchanges the data in the actual request body.

This returns a GET response perfectly. But I have not found any answers to the following:

  • How do I define that my request is a "POST" request instead of a "GET"? I think this is the member I need to use, but I have no idea on how to include it in the code above.
  • How can I include headers and request data (data that my API will extract and consume)?


So again, I apologize for wasting your time, but I have simply not been successful in comprehending the existing documentation and I hope that I can get some pointers, without having to learn C++.

I hope you understand.


Sincerely,
Ragerin
ragerin
 
Posts: 6
Joined: Sat Dec 02, 2017 11:28 am

Re: Possible to send data with HTTP requests?

Postby Hooray » Thu Dec 07, 2017 10:11 pm

The whole thing is mapped to a simgear::HTTP::Request - you can only use what's available in that class, and for that, it must also be exposed to scripting space.

When doing a forum/wiki search, you will learn more about related discussions, feature requests - for example, see:

https://sourceforge.net/p/flightgear/ma ... /34576775/
viewtopic.php?f=30&t=25253

If you think something is missing, it's best to get in touch via the developers mailing list
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU


Return to Nasal

Who is online

Users browsing this forum: No registered users and 4 guests

cron