简体   繁体   English

如何使发布请求具有bas ic auth和内容类型x-www-form-urlencoded

[英]how to make post request baswith ic auth and content type x-www-form-urlencoded

I should make a post request with basic auth and login and password in body (content-type: application/x-www-form-urlencodeed ) I have tried several approaches but it does not work 我应该在正文中使用基本auth以及登录名和密码发出发布请求(内容类型: application/x-www-form-urlencodeed )我尝试了几种方法,但是它不起作用

I tried this, by specifying content-type in a header, but it does not work 我尝试通过在标头中指定content-type来尝试此操作,但是它不起作用

const axios = require('axios')
let session_url = '10.50.141.75:8845/auth'

const requestBody = {
  login:    "node js",
  password: "react js"
}

const config = {
  headers: {
    'Content-type': 'application/x-www-form-urlencodeed'
  },
  auth: {
    username: 'testq',
    password: 'test'
  }
}

axios.post(session_url,requestBody, config).then(function(response) {
  console.log('Authenticated')
}).catch(function(error) {
  console.log(error)
})

I got an error 500, because of the request's body was not correct 由于请求的正文不正确,我收到了500错误

In my opinion you should try: 我认为您应该尝试:

axios.post(session_url, {
  data: requestBody,
  headers: {
    'Accept': 'application/json',
    'Content-type': 'application/json'
  },
  auth: {
     username: 'testq',
     password: 'test'
  }
}).then(function(response) {
  console.log('Authenticated')
}).catch(function(error) {
  console.log(error)
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在+呼叫后内容类型中转义+作为application / x-www-form-urlencoded - How to escape + in post call content type as application/x-www-form-urlencoded 使用 x-www-form-urlencoded content-type 将嵌套的 object 作为 formdata 发布 - post nested object as formdata using x-www-form-urlencoded content-type 如果请求实体的内容类型不是application / x-www-form-urlencoded,则使用@FormParam。 - The @FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded] 如何使用应用程序 / x-www-form-urlencoded 发送 axios 帖子? - How to send axios post with application / x-www-form-urlencoded? 如何使用 Fetch 发布 x-www-form-urlencoded 请求? - How do I POST a x-www-form-urlencoded request using Fetch? 如何使用 Fetch 发布 x-www-form-urlencoded 请求并使用答案? - How do I POST a x-www-form-urlencoded request using Fetch AND work with the answer? 如何在节点js中使用参数为application / x-www-form-urlencoded类型的POST调用 - How to consume POST call where parameter is of type application/x-www-form-urlencoded in node js 在JS中,如何在给定URL和application / x-www-form-urlencoded字符串的情况下进行POST? - In JS, how to make a POST given a URL and a application/x-www-form-urlencoded string? 使用字符串作为请求正文时,为什么 Axios 发送我的 POST 请求时使用 Content-Type application/x-www-form-urlencoded? - Why does Axios send my POST request with Content-Type application/x-www-form-urlencoded when using a string as the request body? 如何使用在Content-Type标头中同时具有“ application / x-www-form-urlencoded”和“ charset = UTF-8”的Javascript发布HTML表单 - How to post a HTML form using Javascript that has both “application/x-www-form-urlencoded” and “charset=UTF-8” in the Content-Type header
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM