axios adds request header

axios adds request header

In front-end development, we often need to use ajax to send HTTP requests. Axios is a popular Promise-based HTTP client library that easily integrates with browsers and HTTP APIs in Node.js. When sending requests using axios, sometimes we need to add some custom request headers to the request. This article will introduce you to how to add request headers in axios and provide some code examples to help you understand better.

What is a request header?
When sending an HTTP request, the request header (Request Header) is a mechanism for appending additional information to the request. It is an object containing various key-value pairs that provide more details about the request, such as the request method, URL, content type, authentication and other information. The format of the request header is usually in the form of key-value pairs. Each key-value pair is separated by a colon, and the key-value pairs are separated by a newline character.

Adding request headers using axios
When sending a request using axios, we can add request headers by configuring the headers attribute of the object. The headers attribute is an object containing request header information, where we can add the required request headers.

Here is an example of using axios to send a GET request and add a custom User-Agent request header to the request header:

import axios from ‘axios’;

axios.get('/api/data', { headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/ 537.3' } }) .then(response => { console.log(response.data); }) .catch(error => { console.error(error); }); 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. In the above example, we added a User-Agent request header to the request header, which is used to identify the client that initiated the request. You can add other custom request headers as needed, such as Authorization, Content-Type, etc.























Adding a public request header
In actual development, we may need to add the same request header to multiple requests. In order to avoid duplicating code, axios allows us to configure a global axios instance, add a request header once, and then include the request header in all requests.

Here is an example of adding public request headers via a global axios instance:

import axios from ‘axios’;

const instance = axios.create({
baseURL: ’
headers: {
‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3’
}
});

instance.get('/api/data')
.then(response => { console.log(response.data); }) .catch(error => { console.error(error); }); 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. In the above example, we created a global axios instance through the axios.create method and configured a The base URL and a User-Agent request header. We can then send requests through this instance and all requests will contain this request header.





















Conclusion
Through this article, we learned how to add request headers in axios. We can add request headers in the configuration object of each request, or add public request headers through the global axios instance. According to actual needs, we can add customized request headers to meet different development needs.

I hope this article will help you understand the request header addition of axios. If you want to learn more about the usage of axios, please check the official documentation of axios.

©Copyright belongs to the author: original work from 51CTO blog author mob649e81583204, please contact the author to obtain reprint authorization, otherwise you will be held legally responsible
axios add request header
https://blog.51cto.com/u_16175449/6932255

Welcome to the Markdown editor

Hello! This is the welcome page displayed when you use the Markdown editor for the first time . If you want to learn how to use the Markdown editor, you can read this article carefully to learn about the basic syntax of Markdown.

new changes

We have made some functional expansion and syntax support for the Markdown editor. In addition to the standard Markdown editor functions, we have added the following new features to help you use it to write blogs:

  1. The new interface design will bring a new writing experience;
  2. Set your favorite code highlighting style in the creation center, and Markdown will display the code piece in the selected highlighting style ;
  3. Added image drag and drop function, you can drag local images directly to the editing area for direct display;
  4. Brand new KaTeX mathematical formula syntax;
  5. Added mermaid syntax 1 function that supports Gantt charts ;
  6. Added the function of editing Markdown articles on multiple screens;
  7. Added functions such as focus writing mode, preview mode, concise writing mode, left and right area synchronized wheel settings , etc. The function button is located between the editing area and the preview area;
  8. Checklist functionality added .

Function shortcut keys

Undo: Ctrl/Command+ Z
Redo: Ctrl/Command+ Y
Bold: Ctrl/Command+ B
Italic: Ctrl/Command+ I
Title: Ctrl/Command+ Shift+ H
Unordered list: Ctrl/Command+ Shift+ U
Ordered list: Ctrl/Command+ Shift+ O
Checklist: + +Insert code Ctrl/Command: Shift+ C
+ Ctrl/CommandInsert Shiftlink K
: Ctrl/Command+ Shift+ L
Insert picture: Ctrl/Command+ Shift+ G
Find: Ctrl/Command+ F
Replace: Ctrl/Command+G

Properly create titles to help create a table of contents

Enter it directly once #and press it, spaceand a level 1 title will be generated.
After entering it twice #and pressing it space, a level 2 title will be generated.
By analogy, we support level 6 titles. TOCHelps to generate a perfect table of contents using syntax.

How to change the style of text

Emphasis on text Emphasis on text

bold text bold text

mark text

Delete text

quoted text

H 2 O is a liquid.

The result of 2 10 operation is 1024.

Insert links and images

Link: link .

picture:Alt

Pictures with dimensions:Alt

Centered image:Alt

Centered and sized image:Alt

Of course, in order to make it more convenient for users, we have added the image drag and drop function.

How to insert a beautiful piece of code

Go to the blog settings page and choose a code fragment highlighting style you like. The same highlighting style is shown below 代码片.

// An highlighted block
var foo = 'bar';

Generate a list that works for you

  • project
    • project
      • project
  1. Project 1
  2. Project 2
  3. Project 3
  • Scheduled Tasks
  • mission accomplished

Create a form

A simple table is created like this:

project Value
computer $1600
cell phone $12
catheter $1

Set content to center, left, or right

Use :---------:center
. Use :----------left.
Use ----------:right.

first row The second column third column
The first column of text is centered The second column of text is on the right The third column of text is on the left

SmartyPants

SmartyPants converts ASCII punctuation characters into "smart" typographic punctuation HTML entities. For example:

TYPE ASCII HTML
Single backticks 'Isn't this fun?' ‘Isn’t this fun?’
Quotes "Isn't this fun?" “Isn’t this fun?”
Dashes -- is en-dash, --- is em-dash – is en-dash, — is em-dash

Create a custom list

Markdown
Text-to- HTML conversion tool
Authors
John
Luke

How to create a footnote

A text with footnotes. 2

Comments are also essential

Markdown converts text to HTML .

KaTeX math formula

You can render LaTeX mathematical expressions using KaTeX :

Gamma infrastructure ( n ) = ( n − 1 ) ! ∀ n ∈ N \Gamma(n) = (n-1)!\quad\forall n\in\mathbb NC ( n )=(n1)!nN is the integral via Euler

Γ ( z ) = ∫ 0 ∞ t z − 1 e − t d t   . \Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,. C ( z )=0tz 1 etdt.

You can find more information about LaTeX mathematical expressions here .

New Gantt chart function to enrich your articles

2014-01-07 2014-01-09 2014-01-11 2014-01-13 2014-01-15 2014-01-17 2014-01-19 2014-01-21 已完成 进行中 计划一 计划二 现有任务 Adding GANTT diagram functionality to mermaid
  • Regarding Gantt chart syntax, please refer here ,

UML diagram

UML diagrams can be used for rendering. Mermaid . For example, a sequence diagram generated below:

张三 李四 王五 你好!李四, 最近怎么样? 你最近怎么样,王五? 我很好,谢谢! 我很好,谢谢! 李四想了很长时间, 文字太长了 不适合放在一行. 打量着王五... 很好... 王五, 你怎么样? 张三 李四 王五

This will produce a flowchart. :

链接
长方形
圆角长方形
菱形
  • Regarding Mermaid syntax, refer here ,

FLowchart flowchart

We will still support flowchart flow charts:

Created with Raphaël 2.3.0 开始 我的操作 确认? 结束 yes no
  • Regarding Flowchart flowchart syntax, refer here .

Export and import

Export

If you want to try using this editor, you can edit whatever you want in this article. When you finish writing an article, find the article export in the upper toolbar and generate a .md file or .html file for local saving.

import

If you want to load a .md file you have written, you can select the import function on the upper toolbar to import the file with the corresponding extension and
continue your creation.


  1. mermaid syntax description↩︎

  2. Explanation of footnote↩︎

Guess you like

Origin blog.csdn.net/gqyuan8888/article/details/134013419