search

Home  >  Q&A  >  body text

node.js - 七牛 callbackUrl callbackBody 不生效

var qiniu = require('qiniu');

qiniu.conf.ACCESS_KEY = 'xxx';
qiniu.conf.ACCESS_KEY = 'xxx';

var bucket = 'test';
var putPolicy  = new qiniu.rs.PutPolicy(bucket);

putPolicy.callbackUrl = 'wokanhao.com:4000';
putPolicy.callbackBody = "name=$(key)";

var token = putPolicy.token();

图片可以上传成功,但是不会回调业务服务器(xxx.com)。
请问我的上传策略中哪里写错了吗?

-----------------------------------继续提问-----------------------------------

修改成如上格式后,qiniu 还是返回 579 错误
请问又是哪里写错了?

-----------------------------------继续提问-----------------------------------

以下两点需要在回调服务器中设置?

3、response header中Content-Type不是'application/json'
4、response body 不是一个合法的json字符串

-----------------------------------继续提问-----------------------------------

putPolicy.callbackUrl = 'http://wokanhao.com:4000/qiniu-callback';
感谢您的提醒,我这里就是先做个测试

您可以访问 http://wokanhao.com:4000/ 随便提交下,
说明这个链接是可以接受POST请求的,并且返回的是JSON格式的数据.

加上上面 putPolicy 的配置

var bucket = 'test';
var putPolicy  = new qiniu.rs.PutPolicy(bucket);
putPolicy.callbackUrl = 'wokanhao.com:4000';
putPolicy.callbackBody = "name=$(key)";

目前还是会出现 图片上传可以成功, 但是出现 579 错误, 并且没有完成回调。

业务服务器返回的JSON需要特定的字段? 还是其他原因?

PHPzPHPz2785 days ago634

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 11:08:41

    The format of callbackBody is different from returnBody. It is a
    "name=$(fname)&hash=$(etag)&location=$(x:location)&price=$(x:price)&uid=123"
    String in the form, reference:
    http://developer.qiniu.com/docs/v6/api/overview/up/response/callback.html

    In addition, looking at your code, putPolicy is assigned a string. Next, a new instance of rs.PutPolicy is created. What is the usage? . .

    If this is nodejs sdk, the normal approach is:

    var putPolicy = new qiniu.rs.PutPolicy(bucket);
    putPolicy.callbackUrl = 'some callbackUrl';
    putPolicy.callbackBody = 'some callbackBody';
    ...
    
    var uptoken = putPolicy.token();
    

    Reference: http://developer.qiniu.com/docs/v6/sdk/nodejs-sdk.html#upload-token

    The reason for the 579 error: Unable to request the callback interface normally, the usual reason:
    1. The interface is not accessible from the public network
    2. The interface does not accept Post method
    3、response header中Content-Type不是'application/json'
    4. The response body is not a legal json string
    5、好吧,再加一个,callbackUrl 不写'http://'

    For how to set the response header, please refer to:
    https://www.google.com.hk/search?q=express response header&oq=express resp&aqs=chrome.3.69i57j0l3.5779j0j1&client=ubuntu-browser&sourceid=chrome&ie=UTF-8

    You also mentioned 您可以访问 http://wokanhao.com:4000/ 随便提交下,, but why doesn’t the callbackUrl include http://?

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 11:08:41

    putPolicy.callbackUrl = 'wokanhao.com:4000';

    This needs to be a url, not a host, it should be changed to:

    putPolicy.callbackUrl = 'http://wokanhao.com:4000/qiniu-callback';

    • Note: This address is only indicative, you should set it according to your actual situation

    reply
    0
  • Cancelreply