简体   繁体   English

使用wget发送urlencoded POST请求

[英]Send urlencoded POST request with wget

The following curl will send a POST request with the url parameter automatically urlencoded: 以下curl将发送带有自动urlencoded的url参数的POST请求:

u="url=http://somewhere.net?param1=val1&param2=val2"
curl -XPOST --data-urlencode "$u" http://example.com

However, I need to issue this request from a system that does not have curl installed. 但是,我需要从没有安装curl的系统发出此请求。

Is there a way to achieve the same with wget ? 有没有办法用wget达到相同的目的? Can $u be urlencoded automatically by wget or another program that might be on a minimal linux installation? wget可能在最小的Linux安装中的其他程序可以自动对$u进行urlencode吗?

as suggested in the comments by @Roadowl, bash itself can urlencode data, the wget invocation could look like 如@Roadowl的评论所建议,bash本身可以对数据进行urlencode,wget调用可能看起来像

wget --post-data=$(printf "url="; urlencode $u) http://example.com

using urlencode implementation from https://gist.github.com/cdown/1163649 , 使用https://gist.github.com/cdown/1163649中的 urlencode实现,

urlencode() {
    # urlencode <string>
    old_lc_collate=$LC_COLLATE
    LC_COLLATE=C

    local length="${#1}"
    for (( i = 0; i < length; i++ )); do
        local c="${1:i:1}"
        case $c in
            [a-zA-Z0-9.~_-]) printf "$c" ;;
            *) printf '%%%02X' "'$c" ;;
        esac
    done

    LC_COLLATE=$old_lc_collate
}

.. probably won't work will null bytes though, given that bash doesn't like null bytes and probably won't allow them in variables ..由于bash不喜欢null字节,并且可能不允许在变量中使用,因此null字节可能不会工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM