【问题标题】:What is the correct way to send json POST request with Gnomes libsoup使用 Gnomes libsoup 发送 json POST 请求的正确方法是什么
【发布时间】:2020-02-17 14:53:10
【问题描述】:

我无法使用 Gnome 的 libsoup 发送 POST 请求。 GET 请求我可以做得很好我只是不确定如何获得有效的帖子请求。

 _httpSession = new Soup.Session();

        let url = "http://localhost:3000/api/auth/register/org";
        var body = {body:"?how to add"}

        let message = Soup.Message.new('POST', url);

        message.set_request('application/json', 2,body);
        _httpSession.queue_message(message, function (_httpSession, message){
            //log res
            global.log(message.response_body.data)

        });

这就是我现在所拥有的。我不知道如何添加帖子请求的正文。文档说 set_request 需要 4 个参数,但我收到一个错误,说如果我添加 body.length,它需要 3 个。

【问题讨论】:

    标签: javascript linux post gnome libsoup


    【解决方案1】:
    
     _httpSession = new Soup.Session();
    
            let url = "http://localhost:3000/api/auth/register/org";
            var body = `{"user":"tom","pass":"1234"}`
    
            let message = Soup.Message.new('POST', url);
    
            message.set_request('application/json', 2,body);
            _httpSession.queue_message(message, function (_httpSession, message){
                //log res
                global.log(message.response_body.data)
    
            });
    

    对于任何试图弄清楚这一点的人来说,body 必须是一个创建为看起来像 json 对象的字符串。我看到的所有例子都只是在说

    body="user=this"
    

    这是不正确的。

    【讨论】:

    • set_request的调用中的“2”是什么?
    • 我知道为时已晚,但万一其他人想知道,“2”描述了 Soup.Buffer 应如何使用传递的数据。查看更多信息:gjs-docs.gnome.org/soup24~2.72.0/soup.memoryuse
    猜你喜欢
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 2020-11-08
    • 2015-08-06
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多