You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Pastebin API发起POST请求?解决invalid api_option报错

解决Pastebin API POST请求的"invalid api_option"错误

嘿,我知道你为啥会碰到这个报错啦!问题出在你把POST请求的参数直接塞到URL里了——Pastebin的API要求POST参数必须放在请求体中,而不是作为URL的查询字符串传递。当你把api_option这类参数拼在URL里时,API根本识别不到它们,自然就会返回"invalid api_option"的错误了。

咱们来一步步修正你的代码:

  1. 先把所有API参数整理成application/x-www-form-urlencoded格式的字符串(这是表单提交的标准格式)
  2. 设置请求的Content-Type头为application/x-www-form-urlencoded,告诉服务器你传递的参数格式
  3. 把参数通过send()方法发送,而不是拼在URL里

修正后的代码如下:

var request = new XMLHttpRequest();
// 只保留基础API地址,参数放到请求体中
request.open("POST", "https://pastebin.com/api/api_post.php", false);
// 设置正确的请求头
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// 整理参数为表单格式的字符串
var params = "api_dev_key=mykey&api_option=paste&api_paste_code=hi";
request.send(params);
console.log(request.response);

另外提个小建议:同步XMLHttpRequest(也就是open方法第三个参数传false)现在已经被浏览器标记为不推荐的做法了,会阻塞主线程导致页面卡顿。如果可以的话,更推荐用异步模式,结合回调处理响应,比如这样:

var request = new XMLHttpRequest();
request.open("POST", "https://pastebin.com/api/api_post.php");
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var params = "api_dev_key=mykey&api_option=paste&api_paste_code=hi";

request.onload = function() {
  if (request.status >= 200 && request.status < 300) {
    console.log("成功!粘贴链接:", request.response);
  } else {
    console.error("请求失败:", request.response);
  }
};

request.send(params);

这样调整之后,API就能正确识别你的api_option参数,应该就能成功创建粘贴啦!

内容的提问来源于stack exchange,提问作者Andrew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:04:22