Hello devs, Please help me.I have this code to get a json text from a server, when I put the url in the web browser I get the Json OK. But in this code I get NULL at response_body.data and length = 0.
Please what I do wrong????
// valac --pkg libsoup-2.4
using Soup;
void main () {
var session = new Soup.SessionAsync ();
var message = new Soup.Message ("GET",
"http://localhost:5800/get_json_list");
session.send_message (message);
message.response_headers.foreach ((name, val) => {
stdout.printf ("Name: %s -> Value: %s\n", name, val);
});
stdout.printf ("Message length: %lld\n%s\n",
message.response_body.length,
message.response_body.data);
}
********server code:
// valac --pkg libsoup-2.4 --thread
public class http_server {
private Soup.Server server;
private string server_name;
private int server_port;
public http_server(string n = "localhost", int p = 5800, string
shared = "/tmp", string theme = "default"){
this.server_port = p;
this.server_name = n;
try{
this.server = new Soup.Server (Soup.SERVER_PORT,
this.server_port);
server.add_handler ("/", default_handler);
string server_full_url = "http://" + this.server_name + ":"
+ this.server_port.to_string() + "/";
print("Iniciando servidor-> %s\n",server_full_url);
this.server.run ();
}catch (Error e) {
warning ("Error %s\n", e.message);
}
}
public void default_handler (Soup.Server server, Soup.Message msg,
string path,
GLib.HashTable? query,
Soup.ClientContext client){
string response_text = """{
"pepe" : 35,
"parents": ["f" : "maria", "m" : "pepe"]
}""";
msg.set_response ("application/json", Soup.MemoryUse.COPY,
response_text.data);
}
}
void main(){
var a = new http_server();
new MainLoop ().run ();
}
Attachment:
smime.p7s
Description: Firma criptográfica S/MIME