编码要发送到Web服务器的查询字符串时 - 何时使用escape()以及何时使用encodeURI()或encodeURIComponent():
使用转义:
escape("% +&=");
Run Code Online (Sandbox Code Playgroud)
要么
使用encodeURI()/ encodeURIComponent()
encodeURI("http://www.google.com?var1=value1&var2=value2");
encodeURIComponent("var1=value1&var2=value2");
Run Code Online (Sandbox Code Playgroud) 在HTTP中有两种POST数据的方式:application/x-www-form-urlencoded和multipart/form-data.据我所知,大多数浏览器只能在使用时上传文件multipart/form-data.在API上下文中使用其中一种编码类型时是否有任何其他指导(不涉及浏览器)?这可能基于:
到目前为止,我基本上没有在网上找到有关使用不同内容类型的正式指导.
我正在将数据发布到外部API(使用PHP,如果它是相关的).
我应该对我传递的POST变量进行URL编码吗?
或者我只需要对GET数据进行URL编码?
谢谢!
更新:这是我的PHP,如果它是相关的:
$fields = array(
'mediaupload'=>$file_field,
'username'=>urlencode($_POST["username"]),
'password'=>urlencode($_POST["password"]),
'latitude'=>urlencode($_POST["latitude"]),
'longitude'=>urlencode($_POST["longitude"]),
'datetime'=>urlencode($_POST["datetime"]),
'category'=>urlencode($_POST["category"]),
'metacategory'=>urlencode($_POST["metacategory"]),
'caption'=>($_POST["description"])
);
$fields_string = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
Run Code Online (Sandbox Code Playgroud) http ×2
post ×2
encoding ×1
http-headers ×1
http-post ×1
javascript ×1
query-string ×1
url-encoding ×1
urlencode ×1