nodejs的post请求json类型及表单类型

json类型:

var https = require('https');
var qs = require('querystring');
var request = require('request');

var post_data = {
    
    
    "text": "百度",
    "index": 0
};
const url = 'https://aip.baidubce.com/rpc/2.0/creation/v1/couplets?';
var token = "";

function postAction(url) {
    
    
    request.post({
    
    
        url: url + "access_token=" + token,
        json: true,
        headers: {
    
    
            'Content-length': post_data.length,
            "Content-Type": "application/x-www-form-urlencoded",
        },
        body: JSON.stringify(post_data)
    },
        (error, response, body) => {
    
    
            if (!error && response.statusCode == 200) {
    
    
                console.log("------------>>>", body);
            }
        }
    );
}

postAction(url);

表单类型:

var https = require('https');
var qs = require('querystring');
var request = require('request');

var post_data = qs.stringify({
    
    
    "news_url": "https://baijiahao.baidu.com/s?id=1691088418716714185",
    "tts_per": 100,
    "duration": 40
});

const url = 'https://aip.baidubce.com/rest/2.0/nlp/v1/create_vidpress?';
var token = "";

function postAction(url) {
    
    
    request.post({
    
    
        url: url + "access_token=" + token,
        headers: {
    
    
            'Content-length': post_data.length,
            "Content-Type": "application/x-www-form-urlencoded",
        },
        form: post_data
    },
        (error, response, body) => {
    
    
            if (!error && response.statusCode == 200) {
    
    
                console.log("------body------>>>", JSON.parse(body));
            }
        }
    );
}

postAction(url);

猜你喜欢

转载自blog.csdn.net/kuilaurence/article/details/113861318