Axios Http 客户端 - 如何使用表单参数构造 Http Post url [英] Axios Http client - How to construct Http Post url with form params

查看:37
本文介绍了Axios Http 客户端 - 如何使用表单参数构造 Http Post url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用要设置的一些表单参数创建 postHTTP 请求.我正在将 axios 与节点服务器一起使用.我已经有一个构建 url 的 java 代码实现,如下所示:

I am trying to create a postHTTP request with some form parameters that are to be set. I am using the axios with node server. I already have a java code implementation of constructing a url as given below:

Java 代码:

HttpPost post = new HttpPost(UriBuilder.fromUri (getProperty("authServerUrl"))
            .path(TOKEN_ACCESS_PATH).build(getProperty("realm")));

List<NameValuePair> formParams = new ArrayList<NameValuePair>();

formParams.add(new NameValuePair("username",getProperty ("username")));
formParams.add(new NameValuePair("password",getProperty ("password")));
formParams.add(new NameValuePair("client_id, "user-client"));

我正在尝试在 axios 中做同样的事情.

I am trying to do the same thing in axios.

AXIOS 实现:

axios.post(authServerUrl +token_access_path,
        {
                username: 'abcd', //gave the values directly for testing
                password: '1235!',
                client_id: 'user-client'
        }).then(function(response) {
            console.log(response); //no output rendered
        }

在 post 请求上设置这些表单参数的方法是否正确?

Is the approach to set these form params on the post request correct?

推荐答案

你必须做到以下几点:

var querystring = require('querystring');
//...
axios.post(authServerUrl + token_access_path,
    querystring.stringify({
            username: 'abcd', //gave the values directly for testing
            password: '1235!',
            client_id: 'user-client'
    }), {
      headers: { 
        "Content-Type": "application/x-www-form-urlencoded"
      }
    }).then(function(response) {
        console.log(response);
    });

这篇关于Axios Http 客户端 - 如何使用表单参数构造 Http Post url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆