【问题标题】:Performing an HTTP POST without encoding a particular parameter执行 HTTP POST 而不编码特定参数
【发布时间】:2020-07-02 10:41:22
【问题描述】:

我正在尝试使用 Node 和请求模块与未记录的第三方 API 对话。

以下代码生成请求:

request.post(
        {
            url: url,
            headers: MY_HEADERS_HERE,
            followAllRedirects: true,
            json: true,
            form: form,
            jar: this.jar,
            gzip: true
        })

我正在向带有 GET 参数的 URL 发送 POST 请求,比如说http://example.com/path?action=/some/action

由于 API 中的一些怪癖,GET 参数及其值也需要以未编码的形式存在,因此 POST 请求的正确主体应该类似于:

parameter1=somedata&parameter2=somedata&action=/some/action

但是,由于 requests 模块对所有表单参数进行了编码,所以正文的内容是:

parameter1=somedata&parameter2=somedata&action=%2Fsome%Faction

有没有办法在请求模块中禁用表单 URL 编码?我现在唯一能想到的就是写一个手动将表单对象转换为查询字符串的方法。

谢谢

托比亚斯·蒂佩

【问题讨论】:

    标签: javascript node.js typescript node-request


    【解决方案1】:

    您可以使用decodeURIComponent(url) 来解码您的参数。也许您应该拆分您的网址,而不是将其用于整个网址。

    request.post(
    {
       url: 'http://example.com/path?' +
            'action=' + decodeURIComponent(actionparam),
       headers: MY_HEADERS_HERE,
       followAllRedirects: true,
       json: true,
       form: form,
       jar: this.jar,
       gzip: true
    

    })

    【讨论】:

    • 我的问题不是 URL 中的参数,而是请求正文中的参数。
    猜你喜欢
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-11
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多