POST array of objects to REST API

后端 未结 2 488
情深已故
情深已故 2021-01-08 00:10

I\'m designing REST API that should be able to accept array of objects, say

[
 {
   \'name\': \'Alice\',
   \'age\': 15
 },
 {
   \'name\': \'Bob\',
   \'age         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-08 00:23

    Basically, the answer I was looking for was:

    1. There is no need to use Content-Type: application/x-www-form-urlencoded which is standard in web; instead, Content-Type: application/json should be used,
    2. The whole HTTP request then looks as follows:

      POST /whatever HTTP/1.1
      Host: api.example.com
      Content-Type: application/json
      
      [
        {
          'name': 'Alice',
          'age': 15
        },
        {
          'name': 'Bob',
          'age': 20
        },
        ...
      ]
      

提交回复
热议问题